Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

How to count GDI handles in .NET windows app

Hi,

Can anyone suggest a tool or a way to findout the controls/widgets that are creating the GDI handles, just like the way Memory Profiler tells the objects that are created and destroyed.
In our application (a huge application)  everytime a form is opened and then closed, there is a leak of 33 GDI handles.
I know that there is a .NET bug which causes a leak of 2 GDI handles for every form that is created and then destroyed.
I am not able to locate which other objects are leaving their GDI handles behing. Because of this issue, our application hangs after the GDI count reaches 3000. For a 24/7 app, in  one 8 hour shift the user has to kill the app every hour and restart.

Any help is greatly appreciated.

Vidit Mathur
Wednesday, December 22, 2004

Does this help?
http://msdn.microsoft.com/msdnmag/issues/01/03/leaks/

Duncan Smart
Wednesday, December 22, 2004

Yeah, the GdiUsage.exe program included with that MSDN article is the easiest way to start to try and figure it out.

You can use it to see the types of GDI objects that were leaked, and also actually visualize the contents of the object itself.  That can be enough to clue you into looking in the right place.

If that doesn't narrow it down enough, you can also try the BoundsChecker program, I seem to remember that it is better at giving you a stack trace of the code that created the leaked object. It can be somewhat painful to use though, because it tends to have a bunch of false positives.

Michael
Friday, December 24, 2004

I recently had to solve a similar problem.  Here's what I did:

1) Searched for 'new'.  Obviously this'll turn up a lot in any app so it's a good idea to do your best to limit the search to the area of code that does the graphics work.  For each instance of 'new', I checked to make sure that those objects that need disposing were being properly disposed.  I use the C# 'using' feature for disposing so it was easy for me to eliminate objects that were correctly disposed of from the search.  It turns out that pretty much every graphics object needs to be disposed -- including things you might not expect like StringFormat and ImageAttributes. 

2) Searched for 'GetH' in order to find calls that return GDI handles such as Bitmap.GetHbitmap and Region.GetHrgn.  The handles returned by these calls must be freed by using DeleteObject through interop.  This turned out to be my biggest source of leaks. 

SomeBody
Friday, December 24, 2004

Thanks for all the responses.
I have the exe that can count the GDI handles. THe one one MSDN is not useful since it runs only on 95/98 and I have XP.

Any other solutions. I have been manually trying to dispose all the forms and tabs and tab controls and that has brought down the count. But I  am really looking for a smarter solution. the solution that I am doing now is just goes against the object oriented principles.

Vidit Mathur
Tuesday, January 04, 2005

> I have the exe that can count the GDI handles.
> THe one one MSDN is not useful since it runs
> only on 95/98 and I have XP.

Oops, when I looked at the link given above, it looked like the right one, but it's old.

Here's the updated one:  http://msdn.microsoft.com/msdnmag/issues/03/01/GDILeaks/default.aspx

This one works with XP - download the GDILeaks.exe, and build the GdiUsage.exe app.

Seeing the actual contents of the objects that are being leaked can help a lot to narrow it down.

Michael
Thursday, January 06, 2005

*  Recent Topics

*  Fog Creek Home