Original Post
My problem is simple... if I define a stateblock using BeginStateBlock(...) and EndStateBlock(...), when I come to release the D3D device, it's reference count in non-zero; i.e. the stateblock does not release properly. If I use ->Capture(...) for the stateblock, it releases OK and the D3D device reference count is zero. I have included the following code to illustrate the problem. Am I doing something wrong?
/**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**///
#define WIN32_LEAN_AND_MEAN
// include
#include "resource.h"
#include "windows.h"
#include "winuser.h"
#include "stdio.h"
#include "stdlib.h"
#include "shlwapi.h"
#include "shellapi.h"
#define D3D_DEBUG_INFO
#include <D3D9.h>
#include <D3D9Types.h>
#include <d3dx9mesh.h>
#include <d3dx9core.h>
/**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**///
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hwnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, NULL);
if (hwnd == NULL)
{
MessageBox(NULL, "FAILED: CreateDialog()", "stateblock test", MB_OK);
return false;
}
IDirect3D9 *pD3D = NULL;
IDirect3DDevice9 *pD3DDevice = NULL;
IDirect3DStateBlock9 *dwSB = NULL;
if((pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
{
pD3D = NULL;
MessageBox(hwnd, "FAILED: Direct3DCreate9()", "stateblock test", MB_OK);
return false;
}
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));
d3dpp.BackBufferWidth = 32;
d3dpp.BackBufferHeight = 32;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
d3dpp.BackBufferCount = 0;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hwnd;
d3dpp.Windowed = true;
d3dpp.EnableAutoDepthStencil = true;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
DWORD dwVertexProcessing;
//dwVertexProcessing = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
dwVertexProcessing = D3DCREATE_MIXED_VERTEXPROCESSING;
if (FAILED (pD3D->CreateDevice(
D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hwnd,
dwVertexProcessing,
&d3dpp,
&pD3DDevice
)))
{
pD3DDevice = NULL;
MessageBox(hwnd, "FAILED: CreateDevice()", "stateblock test", MB_OK);
pD3D->Release();
return false;
}
if (D3D_OK != (pD3DDevice->CreateStateBlock(D3DSBT_ALL, &dwSB)))
{
dwSB = NULL;
MessageBox(hwnd, "FAILED: CreateStateBlock()", "stateblock test", MB_OK);
pD3DDevice->Release();
pD3D->Release();
return false;
}
if (D3D_OK != (pD3DDevice->BeginStateBlock()))
{
MessageBox(hwnd, "FAILED: BeginStateBlock()", "stateblock test", MB_OK);
dwSB->Release();
pD3DDevice->Release();
pD3D->Release();
return false;
}
// Do SB stuff here
if (D3D_OK != (pD3DDevice->EndStateBlock(&dwSB)))
{
MessageBox(hwnd, "FAILED: EndStateBlock()", "stateblock test", MB_OK);
dwSB->Release();
pD3DDevice->Release();
pD3D->Release();
return false;
}
dwSB->Release();
DWORD dwRefCount = pD3DDevice->Release();
pD3D->Release();
char szM[1024];
sprintf(szM, "dwRefCount = %ld", dwRefCount);
MessageBox(hwnd, szM, "stateblock test", MB_OK);
DestroyWindow(hwnd);
EndDialog(hwnd, 0);
return true;
}