Problem with DX in Retail Mode...

Started by
3 comments, last by SteveIDS 16 years, 9 months ago
Hi, I have a GraphicDevice class that wraps a D3D Device. For the initialization I'm using this code:

	D3DPRESENT_PARAMETERS presentParameters;

	ZeroMemory(&presentParameters, sizeof(presentParameters));

	presentParameters.BackBufferWidth				= gameWindow.GetClientWidth();
	presentParameters.BackBufferHeight				= gameWindow.GetClientHeight();
	presentParameters.BackBufferFormat				= D3DFMT_X8R8G8B8;
	presentParameters.BackBufferCount				= 1;
	presentParameters.MultiSampleType				= D3DMULTISAMPLE_NONE;
	presentParameters.MultiSampleQuality			= 0;
	presentParameters.SwapEffect					= D3DSWAPEFFECT_DISCARD;
	presentParameters.Windowed						= true;
	presentParameters.EnableAutoDepthStencil		= true;
	presentParameters.AutoDepthStencilFormat		= D3DFMT_D24S8;
	presentParameters.Flags							= 0;
	presentParameters.FullScreen_RefreshRateInHz	= D3DPRESENT_RATE_DEFAULT;
	presentParameters.PresentationInterval			= D3DPRESENT_INTERVAL_IMMEDIATE;
	presentParameters.hDeviceWindow					= gameWindow.GetWindowHandler();

	IDirect3D9* d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
	assert(d3d9 != NULL);

	D3DCAPS9 caps;
	HRESULT result = d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
	assert(SUCCEEDED(result));

	DWORD vertexProcessing =
		(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
		? D3DCREATE_HARDWARE_VERTEXPROCESSING
		: D3DCREATE_SOFTWARE_VERTEXPROCESSING;

	result = d3d9->CreateDevice(
		D3DADAPTER_DEFAULT,
		D3DDEVTYPE_HAL,
		gameWindow.GetWindowHandler(),
		vertexProcessing,
		&presentParameters,
		&device);

	unsigned long references = d3d9->Release();

	assert(SUCCEEDED(result));
	assert(references == 0);
As you can see, I'm asserting that the references when releasing the D3D9 Interface are zero. In d3d debug mode everything is ok while in retail mode the assertion is triggering with a references value == 1!!! Am I missing something? I'm using the D3D SDK of August 2006... Thanks a lot!
Advertisement
Also, is it actually correct to do this kind of assertion?
Quote:Original post by Puffarbubbole
Also, is it actually correct to do this kind of assertion?
Relying on the result of AddRef() or Release() is a bad idea, so yes - you're probably wrong.

However, I'm more surprised your code works in debug. Standard COM rules should stop an interface being released before all it's clients have been. Your device is a child of the D3D9 object, thus the D3D9 object shouldn't be released before the device is - regardless of what your application does. This would feed into my original assertion that the value returned is not actually that useful - there is nothing in the COM spec that *required* a specific value or class of value from these calls...

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

My ignorance about COM is really evident here =)

I'm releasing the stuff in the correct order now and it works... Still I don't get why I shouldn't rely on the result of Release... I mean, I added an assertion when releasing the device and it was triggering until I removed all the memory leaks so it seemed to work. There is any other way to do this?
The general rule with COM interfaces is to release in the opposite order of creation. The reason being that an interface can create another interface (like D3D creating the device), which increments the reference count. If you release in the wrong order, then one of the releases might not work properly, so you'll have a nonzero reference count, which more than likely gives you a memory leak.

I'm with jollyjeffers, I'm shocked that it even worked in debug.

This topic is closed to new replies.

Advertisement