views:

620

answers:

4

Hello,
Is it a good idea to use GetGuiResources(GetCurrentProcess(),GR_GDIOBJECTS) at the start of winmain, and before the last return to detect GDI leaks or more specifically objects I forgot to release ?
Also I'm currently wondering why the first call in my progam returns 4 when there's no window yet.

+1  A: 

While this will tell you if you've leaked any GDI objects, it won't tell you anything about which GDI objects you've leaked. In a non-trivial program, I don't see this helping very much.

If you want to track GDI objects, I believe that there are some Performance Counters that display the number of GDI object handles that are currently allocated in a process. You could watch that over time to get a better idea of where your program is leaking.

Andy
+1  A: 

The code with this MSDN magazine article provided a slick way to troubleshoot GDI handle leaks. Unfortunately, the source code no longer seems to be available...

Hans Passant
A: 

In most situations, it's enough to use process explorer. Keep the window open showing the GDI handles of your process (right-click on the columns, choose "Select Columns" and then check the "GDI Objects" checkbox in the "Process Memory" tab).

While running your application, watch the number of GDI handles - if it increases and never goes back, you know you have a leak.

Stefan
A: 

On google code, there's a GDI/USER tracking library that can help you track handle usage. It is not user-friendly - rather quickly put together and is dependent on windows debugger but served a good job for me.