C#, Windows Forms app.
We're re-skinning our application and I'm also changing the awful default font we used in the old one. So I thought I'd call the following function to change the font of all controls on a form when the form is loaded.
internal static void SetFonts(Control control)
{
Font oldFont = control.Font;
if (oldFont.Name != GlobalFontName)
{
string familyName = GlobalFontName;
Font newFont = new System.Drawing.Font(familyName,
oldFont.Size, oldFont.Style, GraphicsUnit.Point, 0);
control.Font = newFont;
//oldFont.Dispose();
}
foreach (Control child in control.Controls)
SetFonts(child);
}
I thought it would keep resources down if I disposed of the old font after reassigning the control with the new one, but on closing the form I'm receiving access violation exceptions from one control type from a set of third party controls. If I comment out the line "oldFont.Dispose()" then I don't get the exception.
Is this a bug of the third party control set or is this to be expected?
Memory wise, can I get away with not explicitly disposing of the old font (The app runs on kiosks for 12hr+ a day) ?
Don't dispose of the old Font, that's the job of the Control whose font you're changing. Also, use tools such as GDIView to monitor your handles (such as fonts).