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!