From 974a901f92d5197d3db6356df454bdd47dea39da Mon Sep 17 00:00:00 2001
From: Sebastian Lackner <sebastian@fds-team.de>
Date: Wed, 23 Dec 2015 00:59:07 +0100
Subject: Revert "dsound: Simplify error handling when creating a sound
 buffer."

This reverts commit d51d55bab8995f94dcc78ce8418a4149836c27b0.
---
 dlls/dsound/buffer.c | 46 ++++++++++++++++++++++++++++++----------------
 1 file changed, 30 insertions(+), 16 deletions(-)

diff --git a/dlls/dsound/buffer.c b/dlls/dsound/buffer.c
index d7717fd..5aa2834 100644
--- a/dlls/dsound/buffer.c
+++ b/dlls/dsound/buffer.c
@@ -992,28 +992,29 @@ HRESULT IDirectSoundBufferImpl_Create(
 	LPWAVEFORMATEX wfex = dsbd->lpwfxFormat;
 	HRESULT err = DS_OK;
 	DWORD capf = 0;
-
 	TRACE("(%p,%p,%p)\n",device,pdsb,dsbd);
 
-        *pdsb = NULL;
-
 	if (dsbd->dwBufferBytes < DSBSIZE_MIN || dsbd->dwBufferBytes > DSBSIZE_MAX) {
 		WARN("invalid parameter: dsbd->dwBufferBytes = %d\n", dsbd->dwBufferBytes);
+		*pdsb = NULL;
 		return DSERR_INVALIDPARAM; /* FIXME: which error? */
 	}
 
 	dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
 
-        if (!dsb)
+	if (dsb == 0) {
+		WARN("out of memory\n");
+		*pdsb = NULL;
 		return DSERR_OUTOFMEMORY;
+	}
 
 	TRACE("Created buffer at %p\n", dsb);
 
-        dsb->ref = 1;
+	dsb->ref = 0;
         dsb->refn = 0;
         dsb->ref3D = 0;
         dsb->refiks = 0;
-        dsb->numIfaces = 1;
+	dsb->numIfaces = 0;
 	dsb->device = device;
 	dsb->IDirectSoundBuffer8_iface.lpVtbl = &dsbvt;
         dsb->IDirectSoundNotify_iface.lpVtbl = &dsnvt;
@@ -1024,8 +1025,9 @@ HRESULT IDirectSoundBufferImpl_Create(
 	CopyMemory(&dsb->dsbd, dsbd, dsbd->dwSize);
 
 	dsb->pwfx = DSOUND_CopyFormat(wfex);
-        if (!dsb->pwfx) {
-                IDirectSoundBuffer8_Release(&dsb->IDirectSoundBuffer8_iface);
+	if (dsb->pwfx == NULL) {
+		HeapFree(GetProcessHeap(),0,dsb);
+		*pdsb = NULL;
 		return DSERR_OUTOFMEMORY;
 	}
 
@@ -1050,16 +1052,22 @@ HRESULT IDirectSoundBufferImpl_Create(
 
 	/* Allocate an empty buffer */
 	dsb->buffer = HeapAlloc(GetProcessHeap(),0,sizeof(*(dsb->buffer)));
-        if (!dsb->buffer) {
-                IDirectSoundBuffer8_Release(&dsb->IDirectSoundBuffer8_iface);
+	if (dsb->buffer == NULL) {
+		WARN("out of memory\n");
+		HeapFree(GetProcessHeap(),0,dsb->pwfx);
+		HeapFree(GetProcessHeap(),0,dsb);
+		*pdsb = NULL;
 		return DSERR_OUTOFMEMORY;
 	}
 
 	/* Allocate system memory for buffer */
 	dsb->buffer->memory = HeapAlloc(GetProcessHeap(),0,dsb->buflen);
-        if (!dsb->buffer->memory) {
+	if (dsb->buffer->memory == NULL) {
 		WARN("out of memory\n");
-                IDirectSoundBuffer8_Release(&dsb->IDirectSoundBuffer8_iface);
+		HeapFree(GetProcessHeap(),0,dsb->pwfx);
+		HeapFree(GetProcessHeap(),0,dsb->buffer);
+		HeapFree(GetProcessHeap(),0,dsb);
+		*pdsb = NULL;
 		return DSERR_OUTOFMEMORY;
 	}
 
@@ -1110,12 +1118,18 @@ HRESULT IDirectSoundBufferImpl_Create(
 	/* register buffer if not primary */
 	if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
 		err = DirectSoundDevice_AddBuffer(device, dsb);
-                if (err == DS_OK)
-                        *pdsb = dsb;
-                else
-                        IDirectSoundBuffer8_Release(&dsb->IDirectSoundBuffer8_iface);
+		if (err != DS_OK) {
+			HeapFree(GetProcessHeap(),0,dsb->buffer->memory);
+			HeapFree(GetProcessHeap(),0,dsb->buffer);
+			RtlDeleteResource(&dsb->lock);
+			HeapFree(GetProcessHeap(),0,dsb->pwfx);
+			HeapFree(GetProcessHeap(),0,dsb);
+			dsb = NULL;
+		}
 	}
 
+        IDirectSoundBuffer8_AddRef(&dsb->IDirectSoundBuffer8_iface);
+	*pdsb = dsb;
 	return err;
 }
 
-- 
2.6.4

