In my project, I have created around 12 custom controls(checkbox,radiobutton,editbox,progressbar,scrollbar,button etc...). Custom painting is done in all controls for the extended look and feel using Pen,Brushes,Graphics, GraphicPath,Images,Region and Bitmaps ect. After the use of these objects, I have disposed the objects using the Dispose() method. I am using these controls in a System.Windows Form. When I am contiously showing and closing the Form causes an increase in the GDI objecs each time. I have checked the GDI object count using Task Manager and confirmed the GDI leak in the form.
My question is, how can I manage this GDI leak?
How do I find which GDI object causes the leak eventhough I am disposing the GDI objects?

Just because you Disposed of an object doesn't mean the system has. Keep opening/closing the form and see if you run out of memory (you really do have a leak) or at some point all those GDI objects vanish (the GC ran and cleaned up for you).

Opening and closing form simple keeps an increase in GDI object count in the Task Manager.
In the code I am disposing all objects like Pen,Brush, Region, Bitmaps etc. But there is an increase in the GDI count. How do I find which Object causes the leak?

Edited 4 Years Ago by kaizen202

Finally I got the object which causes the leak!! Thanks to Bear(http://thesz.diecru.eu/content/bear.php). The Font object in some custom controls are not get disposed even if the control's Dispose() method is called. The GDI object(Font) count keeps increasing with 1. Anyone know, why the Font is not disposing properly. How can I dispose the font object when destroying the control?

This article has been dead for over six months. Start a new discussion instead.