[win32] GDI, Double Buffering, and Memory Leaks

Started by
3 comments, last by ScottMayo 14 years, 9 months ago
MSDN, always helpful, says the following:
Quote:When the memory DC is created, its display surface is exactly one monochrome pixel wide and one monochrome pixel high.
When using a back buffer for double-buffering, you'll obviously need to replace that default display surface with another bitmap. This may be silly, but my question is this: will calling DeleteDC() for that back buffer on exit without restoring the original display surface cause a memory leak (however small it may be)?
Advertisement
On exit? Do you mean when the process exits? If so, then no. The OS reclaims the resources used by a process when that process exits. gdiobj.zip is a free utility from the author of "Windows Graphics Programming: Win32 GDI and DirectDraw" - It can help you check for leaking gdi objects, dc, bitmap, etc. Use it to run some tests on programs that you think might leak.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Quote:Original post by LessBread
On exit? Do you mean when the process exits? If so, then no. The OS reclaims the resources used by a process when that process exits. gdiobj.zip is a free utility from the author of "Windows Graphics Programming: Win32 GDI and DirectDraw" - It can help you check for leaking gdi objects, dc, bitmap, etc. Use it to run some tests on programs that you think might leak.


Yes, I did mean when the process exits.

The reason I ask this question is because I was looking at an example of double-buffering using regular GDI. It saved off the "old" (i.e. default) bitmap of the back buffer when selecting a newly created bitmap into it. When the game exits, the code restores the old bitmap into the back buffer -- just before deleting the back buffer. Maybe I'm naive, but it seems a bit silly to me to have yet another global variable just for saving an old bitmap which takes up a single monochrome pixel's worth of memory. But if there is a compelling reason for doing so, please let me know.

Thanks for your input! I will give that utility a spin. :)
It might be silly for your special case, but it's an extremely bad habit to get in to. All the damn time I see people getting lazy about cleaning up after themselves and then doing something like not restoring the old GDI object, deleting the DC, trying to use the original object handle, and then blaming Microsoft when they have trouble with thier code.
-Mike
Process exit forgives a multitude of sins in Windows (at least since 95 or so), but if you use a language anything like C++, use RAII techniques to make sure you give back everything you take. Or, better, find a package that takes care of it for you. Resource management in Windows tends to be slightly quirky and nothing you want to get too close to.

This topic is closed to new replies.

Advertisement