2

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 program returns 4 when there's no window yet.

Wolf
  • 9,679
  • 7
  • 62
  • 108
Zub
  • 53
  • 1
  • 5
  • see also the related question [*How to get GDI objects associated to a process*](https://stackoverflow.com/questions/251343/how-to-get-gdi-objects-associated-to-a-process) – Wolf Feb 10 '17 at 13:58

3 Answers3

2

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
  • 43,293
  • 10
  • 75
  • 117
1

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
  • 922,412
  • 146
  • 1,693
  • 2,536
0

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
  • 30,088
  • 6
  • 78
  • 89