Jump to content
  • Advertisement
Sign in to follow this  
aniskhalife

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

This topic is 3256 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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; }

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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 ????

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!