if i call CreateDevice in loop, it gives GDI obect handle leak.

Started by
14 comments, last by Evil Steve 14 years, 6 months ago
if i call CreateDevice in loop, it gives GDI obect handle leak. is there any problem with the below code ?????? please help int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; // initialize MFC and print and error on failure if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { // TODO: change error code to suit your needs _tprintf(_T("Fatal Error: MFC initialization failed\n")); nRetCode = 1; } else { // TODO: code your application's behavior here. IDirect3D9* g_pD3D9 = NULL; IDirect3DDevice9* g_pDevRealTime = NULL; DWORD dwBehaviorFlags = 0; D3DPRESENT_PARAMETERS g_D3DPresentParameters = {0}; g_pD3D9 = Direct3DCreate9(D3D_SDK_VERSION); if( g_pD3D9 == NULL ) { return E_OUTOFMEMORY; } D3DPRESENT_PARAMETERS g_D3DPresentParameters = {0}; ZeroMemory( &g_D3DPresentParameters, sizeof(D3DPRESENT_PARAMETERS) ); g_D3DPresentParameters.hDeviceWindow = g_hWnd; g_D3DPresentParameters.Windowed = TRUE; g_D3DPresentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD; g_D3DPresentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT; g_D3DPresentParameters.BackBufferWidth = 1; g_D3DPresentParameters.BackBufferHeight = 1; g_D3DPresentParameters.BackBufferCount = 1; g_D3DPresentParameters.MultiSampleType = D3DMULTISAMPLE_NONE ; dwBehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING; UINT iLoop = 1; while(iLoop <= 100) { HRESULT hrCreateDevice = g_pD3D9->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hDeskWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pDevRealTime ); printf("\nParameters Passed to CreateDevice() API:"); printf(" CreateDevice(%d,%d,NULL,0x%x,0x%u)",D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, dwBehaviorFlags, g_D3DPresentParameters); printf("\nThe Return Value of CreateDevice() API:0x%x ", hrCreateDevice); switch(hrCreateDevice) { case D3DERR_INVALIDCALL: printf("\nInvalid Call Error"); break; case D3DERR_INVALIDDEVICE: printf("\nInvalid Device Error"); break; case D3DERR_OUTOFVIDEOMEMORY: printf("\nOutofVideoMemory Error"); break; case D3DERR_NOTAVAILABLE: printf("\nDevice Not Available Error"); break; case D3DERR_DEVICELOST: printf("\nDevice Lost Error"); break; case S_OK: printf("\nCreateDevice() API executed successfully"); break; } if(hrCreateDevice != S_OK) { printf("Error"); break; } if g_pDevRealTime->Release(); g_pDevRealTime = NULL; Sleep(500); iLoop++; } g_pD3D9->Release(); g_pD3D9 = NULL; } return nRetCode; }
Advertisement
Why would you do this?!?

Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

Ummm...I'm not surprised. In general there's very few reasons to create more than one D3D9 device...in some cases apps will create 2 or 3 for multiple monitors. Creating 100 is just plain crazy.
MJP >> yes its very true not to try such things but my intension is to check the return value of CreateDevice when called multiple times ..
but my question is why it gives memory or GDI leak ????
Quote:Original post by aniskhalife
MJP >> yes its very true not to try such things but my intension is to check the return value of CreateDevice when called multiple times ..
but my question is why it gives memory or GDI leak ????
I'd assume there's some state that's not being cleaned up internally, either in the D3D code, or in the driver.

Instead of trying something and seeing if it fails - which could produce all sorts of horrible artifacts on screen such as the screen flickering or displaying corruption - you should check the device capabilities, then create the device correctly first time.

How are you determining there's a GDI handle leak exactly?
About GDI leak, i came to know it from TASK MANAGER..
What rate does it leak at? One handle per call? Does it leak linearly (A constant number of handles per iteration)? Does it still leak if you release the IDirect3D9 object too?
yes, one handle per call and yes even if i release IDirect3D9 object
One handle per call is not really anything to worry about. As mentioned before, you shouldn't be calling CreateDevice more than 2 or 3 times max anyway.
My code is something like this.

.
.
.
while(iLoop <= 100)
{
HRESULT hrCreateDevice = g_pD3D9->CreateDevice( D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,hDeskWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,&g_pDevRealTime );
.
.
.
g_pDevRealTime->Release();
g_pDevRealTime = NULL;
iLoop++;
}
.
.
.

When i am executing the same code on XP then 1 GDI object leaks per loop in Task Manager, and on Vista/W7 it should memory leak of about 100 KB per loop.

My question is whether g_pD3D9->CreateDevice(...) API is causing internal memory leaks or not.

This topic is closed to new replies.

Advertisement