Skip to main content
gamedev.net
🔒 Locked

HDCs and HBITMAPs

Started by Progames25 Mar 18, 2009 at 6:26 PM 4 replies 950+ views
Original Post
Progames25
Progames25
Hello, Well, I don' know very much about HDCs and HBITMAPS. My question is, is it possible to have a memory leak using HDCs and HBITMAPs? In my program I use multiple HDCs and HBITMAPS that are used in the WM_PAINT message and I want to know if it is possible to have a memory leak from one of these if I don't properly delete them (DeleteObject(),DeleteDC()...) or forget to delete one at one point? I also wonder if it is necessary to call ReleaseDC() each time before calling DeleteDC()? If you can get a memory leak from not deleting a DC or HBITMAP (DeleteObject), is there an east way to find out if you have a leak? I tried adding a few lines of code inside my program's WM_DESTROY message that checks:

if (hdcMem)
{
	MessageBox(NULL,L"hdcMem not deleted?",L"hdcMem",MB_OK);
}


Something like that so it tells me something if that one HC has not been deleted and possibly a memory leak...but I always got that message box even if I called DeleteDC and ReleaseDC before the if statement and I figure that is the way I worded the argument in the if statement so I tried if(hdcMem != NULL). So, like I said I don' know much about how all that wroks so if you could give me some information about this that would be great. I tried searching for some things online and the only thing I found was ReleaseDC so... Thanks, Progames25
-----------------------------I use C++ and DirectX.I'm new to game programming.
LessBread
LessBread
Yes, it's possible to leak memory through too many unreclaimed HDC and HBITMAP objects. A free helpful utility for checking if your program is leaking that way is Feng Yuan's GDIObj.exe (gdiobj.zip).

The MSDN documentation for the various GDI routines is helpful in figuring out how to properly clean up GDI objects. Feng's book is really good too.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
george135
george135
You don't need any tool.
Just read the internal GDI handle table.
Evil Steve
Evil Steve
Quote:
Original post by Progames25
Hello,
Well, I don' know very much about HDCs and HBITMAPS. My question is, is it possible to have a memory leak using HDCs and HBITMAPs? In my program I use multiple HDCs and HBITMAPS that are used in the WM_PAINT message and I want to know if it is possible to have a memory leak from one of these if I don't properly delete them (DeleteObject(),DeleteDC()...) or forget to delete one at one point?
As others have said; Yes this is entirely possible.

Quote:
Original post by Progames25
I also wonder if it is necessary to call ReleaseDC() each time before calling DeleteDC()?
No - You only use ReleaseDC() if you got the DC from a GetDC() call. You only use DeleteDC() if you got it from a CreateDC() or CreateCompatibleDC() call.

Quote:
Original post by Progames25
If you can get a memory leak from not deleting a DC or HBITMAP (DeleteObject), is there an east way to find out if you have a leak? I tried adding a few lines of code inside my program's WM_DESTROY message that checks:
*** Source Snippet Removed ***
Something like that so it tells me something if that one HC has not been deleted and possibly a memory leak...but I always got that message box even if I called DeleteDC and ReleaseDC before the if statement and I figure that is the way I worded the argument in the if statement so I tried if(hdcMem != NULL).
That won't help if you have two copies of the handle, and only close one. You can open up task manager, go to View -> Select Columns, and turn on GDI objects. Then if you object count keeps going up as your app runs, you probably have a leak.
LessBread
LessBread
Feng Yuan's tool differentiates between different types of GDI objects. So rather than simply finding out that you've got a leak when the count of GDI objects increases, as with the task manager, you can see exactly which kind of GDI object is leaking, DC, Region, Bitmap, Palette, Font, Brush, so that you can focus your debugging efforts on the actual problem, rather than an imagined one. That is, for example, if your GDI Object count is soaring and you think it's a DC problem and you spend all your time fussing with DC, except it's really a Brush problem, then you've wasted your time fussing with DC when you should have been working on Brushes. If newer versions of Windows provide that level of detail, then nevermind.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Progames25
Progames25
Awsome,
That GDIObj.exe tool is really great and I never knew Task Manager showed GDI, thanks for the help. So far it looks like I am OK so, thanks for helping me out with this :)

Quote:
"No - You only use ReleaseDC() if you got the DC from a GetDC() call. You only use DeleteDC() if you got it from a CreateDC() or CreateCompatibleDC() call."

Thanks for that information. I can take a few lines of code out now :)


Thanks,
Progames25
-----------------------------I use C++ and DirectX.I'm new to game programming.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.