GDI Handle leak when adding a simple Textbox

P

Pranil Kanderi

I have a huge application with lots of custom controls. The app had a
memory leak but fixed that by calling the dispose method where needed
and now the user objects are very stable, when I am looking at the task
manager. But the GDI handles keep growing. I narrowed it down to having
System.Drawing.Font in my custom controls.

To test it out:
I created a simple form with two buttons Add and Remove that - adds and
removes a simple textbox. For this textbox I have created a class that
inherits System.Windows.Forms.UserControl that only has one text box.

Now when the text box doesnot have a Font the GDI handles does not
increase. But when I use the below GDI handles increase:

Me.TextBox1.Font = New System.Drawing.Font("Arial Black", 9.0!,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
CType(0, Byte))

In the Dispose method of this custom control class I have tried this
but the GDI handles keep increasing and after a while (GC) it drops
down, but not to the original number where it started, but with 1 more
than the original GDI count.

Me.TextBox1.Font.FontFamily.Dispose()
Me.TextBox1.Font.Dispose()
Me.TextBox1.Font.FontFamily.Equals(Nothing)
Me.TextBox1.Font = Nothing
Me.TextBox1.Dispose()


I need the text box in a UserControl class because I have lot of custom
controls but with many more controls.

Any help is highly appreciated.

Thanks!
 
C

Chris Dunaway

Pranil said:
Now when the text box doesnot have a Font the GDI handles does not
increase. But when I use the below GDI handles increase:

Me.TextBox1.Font = New System.Drawing.Font("Arial Black", 9.0!,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
CType(0, Byte))

How can it not have a font? When the textbox is first created, does it
not have a default font? When you assign a new font, could the
previous font be hanging around, taking a GDI handle? What if you
disposed the existing font before assigning it?

Me.TextBox1.Font.Dispose()
Me.TextBox1.Font = New System.Drawing.Font(...)

Just a thought.
Me.TextBox1.Font.FontFamily.Equals(Nothing)

By the way, the line above doesn't do anything! The Equals method only
returns a boolean. It doesn't change anything about the FontFamily
object.

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top