Sign in to follow this  
RobAU78

[win32] GDI, Double Buffering, and Memory Leaks

Recommended Posts

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)?

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this