Commit 915174d8 authored by Stefan Dösinger's avatar Stefan Dösinger Committed by Alexandre Julliard

ddraw: Palette refcounting fix.

Do not AddRef the DirectDraw interfaces in CreatePalette of older interface version, and add a test for that.
parent 0a812957
...@@ -3007,6 +3007,7 @@ IDirectDrawImpl_CreatePalette(IDirectDraw7 *iface, ...@@ -3007,6 +3007,7 @@ IDirectDrawImpl_CreatePalette(IDirectDraw7 *iface,
} }
IDirectDraw7_AddRef(iface); IDirectDraw7_AddRef(iface);
object->ifaceToRelease = (IUnknown *) iface;
*Palette = ICOM_INTERFACE(object, IDirectDrawPalette); *Palette = ICOM_INTERFACE(object, IDirectDrawPalette);
return DD_OK; return DD_OK;
} }
......
...@@ -393,6 +393,7 @@ struct IDirectDrawPaletteImpl ...@@ -393,6 +393,7 @@ struct IDirectDrawPaletteImpl
/* IDirectDrawPalette fields */ /* IDirectDrawPalette fields */
IDirectDrawImpl *ddraw_owner; IDirectDrawImpl *ddraw_owner;
IUnknown *ifaceToRelease;
}; };
const IDirectDrawPaletteVtbl IDirectDrawPalette_Vtbl; const IDirectDrawPaletteVtbl IDirectDrawPalette_Vtbl;
......
...@@ -236,10 +236,13 @@ IDirectDrawImpl_CreatePalette(LPDIRECTDRAW This, DWORD dwFlags, ...@@ -236,10 +236,13 @@ IDirectDrawImpl_CreatePalette(LPDIRECTDRAW This, DWORD dwFlags,
dwFlags, pEntries, ppPalette, pUnkOuter); dwFlags, pEntries, ppPalette, pUnkOuter);
if(SUCCEEDED(hr) && *ppPalette) if(SUCCEEDED(hr) && *ppPalette)
{ {
IDirectDrawPaletteImpl *impl = ICOM_OBJECT(IDirectDrawPaletteImpl, IDirectDrawPalette, *ppPalette);
IDirectDraw7_Release(COM_INTERFACE_CAST(IDirectDrawImpl, IDirectDraw7_Release(COM_INTERFACE_CAST(IDirectDrawImpl,
IDirectDraw, IDirectDraw,
IDirectDraw7, IDirectDraw7,
This)); This));
impl->ifaceToRelease = NULL;
} }
return hr; return hr;
} }
...@@ -251,18 +254,21 @@ IDirectDraw2Impl_CreatePalette(LPDIRECTDRAW2 This, DWORD dwFlags, ...@@ -251,18 +254,21 @@ IDirectDraw2Impl_CreatePalette(LPDIRECTDRAW2 This, DWORD dwFlags,
IUnknown *pUnkOuter) IUnknown *pUnkOuter)
{ {
HRESULT hr; HRESULT hr;
return IDirectDraw7_CreatePalette(COM_INTERFACE_CAST(IDirectDrawImpl, hr = IDirectDraw7_CreatePalette(COM_INTERFACE_CAST(IDirectDrawImpl,
IDirectDraw2, IDirectDraw2,
IDirectDraw7, IDirectDraw7,
This), This),
dwFlags, pEntries, ppPalette, pUnkOuter); dwFlags, pEntries, ppPalette, pUnkOuter);
if(SUCCEEDED(hr) && *ppPalette) if(SUCCEEDED(hr) && *ppPalette)
{ {
IDirectDrawPaletteImpl *impl = ICOM_OBJECT(IDirectDrawPaletteImpl, IDirectDrawPalette, *ppPalette);
IDirectDraw7_Release(COM_INTERFACE_CAST(IDirectDrawImpl, IDirectDraw7_Release(COM_INTERFACE_CAST(IDirectDrawImpl,
IDirectDraw, IDirectDraw2,
IDirectDraw7, IDirectDraw7,
This)); This));
impl->ifaceToRelease = NULL;
} }
return hr;
} }
static HRESULT WINAPI static HRESULT WINAPI
...@@ -272,18 +278,22 @@ IDirectDraw4Impl_CreatePalette(LPDIRECTDRAW4 This, DWORD dwFlags, ...@@ -272,18 +278,22 @@ IDirectDraw4Impl_CreatePalette(LPDIRECTDRAW4 This, DWORD dwFlags,
IUnknown *pUnkOuter) IUnknown *pUnkOuter)
{ {
HRESULT hr; HRESULT hr;
return IDirectDraw7_CreatePalette(COM_INTERFACE_CAST(IDirectDrawImpl, hr = IDirectDraw7_CreatePalette(COM_INTERFACE_CAST(IDirectDrawImpl,
IDirectDraw4, IDirectDraw4,
IDirectDraw7, IDirectDraw7,
This), This),
dwFlags, pEntries, ppPalette, pUnkOuter); dwFlags, pEntries, ppPalette, pUnkOuter);
if(SUCCEEDED(hr) && *ppPalette) if(SUCCEEDED(hr) && *ppPalette)
{ {
IDirectDrawPaletteImpl *impl = ICOM_OBJECT(IDirectDrawPaletteImpl, IDirectDrawPalette, *ppPalette);
IDirectDraw7_Release(COM_INTERFACE_CAST(IDirectDrawImpl, IDirectDraw7_Release(COM_INTERFACE_CAST(IDirectDrawImpl,
IDirectDraw, IDirectDraw4,
IDirectDraw7, IDirectDraw7,
This)); This));
IDirectDraw4_AddRef(This);
impl->ifaceToRelease = (IUnknown *) This;
} }
return hr;
} }
static HRESULT WINAPI static HRESULT WINAPI
......
...@@ -21,6 +21,8 @@ ...@@ -21,6 +21,8 @@
#include "winerror.h" #include "winerror.h"
#include "wine/debug.h" #include "wine/debug.h"
#define COBJMACROS
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
...@@ -104,7 +106,10 @@ IDirectDrawPaletteImpl_Release(IDirectDrawPalette *iface) ...@@ -104,7 +106,10 @@ IDirectDrawPaletteImpl_Release(IDirectDrawPalette *iface)
if (ref == 0) if (ref == 0)
{ {
IWineD3DPalette_Release(This->wineD3DPalette); IWineD3DPalette_Release(This->wineD3DPalette);
IDirectDraw7_Release(ICOM_INTERFACE(This->ddraw_owner, IDirectDraw7)); if(This->ifaceToRelease)
{
IUnknown_Release(This->ifaceToRelease);
}
HeapFree(GetProcessHeap(), 0, This); HeapFree(GetProcessHeap(), 0, This);
} }
......
...@@ -47,32 +47,42 @@ static void test_ddraw_objects(void) ...@@ -47,32 +47,42 @@ static void test_ddraw_objects(void)
{ {
HRESULT hr; HRESULT hr;
unsigned long ref; unsigned long ref;
IDirectDraw7 *DDraw; IDirectDraw7 *DDraw7;
IDirectDraw4 *DDraw4;
IDirectDraw4 *DDraw2;
IDirectDraw4 *DDraw1;
IDirectDrawPalette *palette; IDirectDrawPalette *palette;
IDirectDrawSurface7 *surface; IDirectDrawSurface7 *surface;
PALETTEENTRY Table[256]; PALETTEENTRY Table[256];
DDSURFACEDESC2 ddsd; DDSURFACEDESC2 ddsd;
hr = pDirectDrawCreateEx(NULL, (void **) &DDraw, &IID_IDirectDraw7, NULL); hr = pDirectDrawCreateEx(NULL, (void **) &DDraw7, &IID_IDirectDraw7, NULL);
ok(hr == DD_OK || hr==DDERR_NODIRECTDRAWSUPPORT, "DirectDrawCreateEx returned: %lx\n", hr); ok(hr == DD_OK || hr==DDERR_NODIRECTDRAWSUPPORT, "DirectDrawCreateEx returned: %lx\n", hr);
if(!DDraw) if(!DDraw7)
{ {
trace("Couldn't create DDraw interface, skipping tests\n"); trace("Couldn't create DDraw interface, skipping tests\n");
return; return;
} }
ref = getRefcount( (IUnknown *) DDraw); hr = IDirectDraw7_QueryInterface(DDraw7, &IID_IDirectDraw4, (void **) &DDraw4);
ok(hr == DD_OK, "IDirectDraw7_QueryInterface returned %08lx\n", hr);
hr = IDirectDraw7_QueryInterface(DDraw7, &IID_IDirectDraw2, (void **) &DDraw2);
ok(hr == DD_OK, "IDirectDraw7_QueryInterface returned %08lx\n", hr);
hr = IDirectDraw7_QueryInterface(DDraw7, &IID_IDirectDraw, (void **) &DDraw1);
ok(hr == DD_OK, "IDirectDraw7_QueryInterface returned %08lx\n", hr);
ref = getRefcount( (IUnknown *) DDraw7);
ok(ref == 1, "Got refcount %ld, expected 1\n", ref); ok(ref == 1, "Got refcount %ld, expected 1\n", ref);
/* Fails without a cooplevel */ /* Fails without a cooplevel */
hr = IDirectDraw7_CreatePalette(DDraw, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, Table, &palette, NULL); hr = IDirectDraw7_CreatePalette(DDraw7, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, Table, &palette, NULL);
ok(hr == DDERR_NOCOOPERATIVELEVELSET, "CreatePalette returned %08lx\n", hr); ok(hr == DDERR_NOCOOPERATIVELEVELSET, "CreatePalette returned %08lx\n", hr);
/* This check is before the cooplevel check */ /* This check is before the cooplevel check */
hr = IDirectDraw7_CreatePalette(DDraw, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, Table, &palette, (void *) 0xdeadbeef); hr = IDirectDraw7_CreatePalette(DDraw7, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, Table, &palette, (void *) 0xdeadbeef);
ok(hr == CLASS_E_NOAGGREGATION, "CreatePalette returned %08lx\n", hr); ok(hr == CLASS_E_NOAGGREGATION, "CreatePalette returned %08lx\n", hr);
hr = IDirectDraw7_SetCooperativeLevel(DDraw, 0, DDSCL_NORMAL); hr = IDirectDraw7_SetCooperativeLevel(DDraw7, 0, DDSCL_NORMAL);
ok(hr == DD_OK, "SetCooperativeLevel failed with %08lx\n", hr); ok(hr == DD_OK, "SetCooperativeLevel failed with %08lx\n", hr);
memset(&ddsd, 0, sizeof(ddsd)); memset(&ddsd, 0, sizeof(ddsd));
...@@ -85,22 +95,22 @@ static void test_ddraw_objects(void) ...@@ -85,22 +95,22 @@ static void test_ddraw_objects(void)
U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB; U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB;
U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 8; U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 8;
hr = IDirectDraw7_CreateSurface(DDraw, &ddsd, &surface, NULL); hr = IDirectDraw7_CreateSurface(DDraw7, &ddsd, &surface, NULL);
ok(hr == DD_OK, "CreateSurface failed with %08lx\n", hr); ok(hr == DD_OK, "CreateSurface failed with %08lx\n", hr);
/* DDraw refcount increased by 1 */ /* DDraw refcount increased by 1 */
ref = getRefcount( (IUnknown *) DDraw); ref = getRefcount( (IUnknown *) DDraw7);
ok(ref == 2, "Got refcount %ld, expected 2\n", ref); ok(ref == 2, "Got refcount %ld, expected 2\n", ref);
/* Surface refcount starts with 1 */ /* Surface refcount starts with 1 */
ref = getRefcount( (IUnknown *) surface); ref = getRefcount( (IUnknown *) surface);
ok(ref == 1, "Got refcount %ld, expected 1\n", ref); ok(ref == 1, "Got refcount %ld, expected 1\n", ref);
hr = IDirectDraw7_CreatePalette(DDraw, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, Table, &palette, NULL); hr = IDirectDraw7_CreatePalette(DDraw7, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, Table, &palette, NULL);
ok(hr == DD_OK, "CreatePalette returned %08lx\n", hr); ok(hr == DD_OK, "CreatePalette returned %08lx\n", hr);
/* DDraw refcount increased by 1 */ /* DDraw refcount increased by 1 */
ref = getRefcount( (IUnknown *) DDraw); ref = getRefcount( (IUnknown *) DDraw7);
ok(ref == 3, "Got refcount %ld, expected 3\n", ref); ok(ref == 3, "Got refcount %ld, expected 3\n", ref);
/* Palette starts with 1 */ /* Palette starts with 1 */
...@@ -119,7 +129,7 @@ static void test_ddraw_objects(void) ...@@ -119,7 +129,7 @@ static void test_ddraw_objects(void)
IDirectDrawSurface7_Release(surface); IDirectDrawSurface7_Release(surface);
/* Incresed before - decrease now */ /* Incresed before - decrease now */
ref = getRefcount( (IUnknown *) DDraw); ref = getRefcount( (IUnknown *) DDraw7);
ok(ref == 2, "Got refcount %ld, expected 2\n", ref); ok(ref == 2, "Got refcount %ld, expected 2\n", ref);
/* Releasing the surface detaches the palette */ /* Releasing the surface detaches the palette */
...@@ -129,10 +139,34 @@ static void test_ddraw_objects(void) ...@@ -129,10 +139,34 @@ static void test_ddraw_objects(void)
IDirectDrawPalette_Release(palette); IDirectDrawPalette_Release(palette);
/* Incresed before - decrease now */ /* Incresed before - decrease now */
ref = getRefcount( (IUnknown *) DDraw); ref = getRefcount( (IUnknown *) DDraw7);
ok(ref == 1, "Got refcount %ld, expected 1\n", ref); ok(ref == 1, "Got refcount %ld, expected 1\n", ref);
IDirectDraw7_Release(DDraw); /* Not all interfaces are AddRefed when a palette is created */
hr = IDirectDraw4_CreatePalette(DDraw4, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, Table, &palette, NULL);
ok(hr == DD_OK, "CreatePalette returned %08lx\n", hr);
ref = getRefcount( (IUnknown *) DDraw4);
ok(ref == 2, "Got refcount %ld, expected 2\n", ref);
IDirectDrawPalette_Release(palette);
/* No addref here */
hr = IDirectDraw2_CreatePalette(DDraw2, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, Table, &palette, NULL);
ok(hr == DD_OK, "CreatePalette returned %08lx\n", hr);
ref = getRefcount( (IUnknown *) DDraw2);
ok(ref == 1, "Got refcount %ld, expected 1\n", ref);
IDirectDrawPalette_Release(palette);
/* No addref here */
hr = IDirectDraw_CreatePalette(DDraw1, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, Table, &palette, NULL);
ok(hr == DD_OK, "CreatePalette returned %08lx\n", hr);
ref = getRefcount( (IUnknown *) DDraw1);
ok(ref == 1, "Got refcount %ld, expected 1\n", ref);
IDirectDrawPalette_Release(palette);
IDirectDraw7_Release(DDraw7);
IDirectDraw4_Release(DDraw4);
IDirectDraw2_Release(DDraw2);
IDirectDraw_Release(DDraw1);
} }
static void test_iface_refcnt(void) static void test_iface_refcnt(void)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment