Click here to Skip to main content
15,886,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Greetings, I have a C# windows form application with text and the font code is apparently auto generated and placed in the Form.Designer.cs file. I've been told that I need to ensure the font is disposed correctly but since it is auto generated code, I am unsure as to how to go about it. So, I searched and found this:

You can control the disposal of Font by including in using construct pattern. In the calling main, you should wrap the Application inside the using construct of the Form.

Example:

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FrmMainUtility());

to this:

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
using (FrmMainUtility form = new FrmMainUtility())
{
Application.Run(form);
}

Does this look correct to you? Is this a technique I can use to accomplish a clean disposal of the fonts?

I realize that there are Dispose methods that are standard practice, but I'd like to know if the above code works to dispose the fonts.
Note: My app has been subject to a HP Fortify scan. This example taken from :
http://stackoverflow.com/questions/21769908/how-and-when-is-font-disposed-for-winforms-controls[^]
Posted
Updated 31-Dec-14 2:29am
v3
Comments
BillWoodruff 31-Dec-14 5:18am    
I am not sure if ... at this point in time ... a WinForms app really needs extra code to properly dispose of Fonts specified at design-time.

Do you have a link to any source that provides an explanation of this ?
Philippe Mori 31-Dec-14 11:01am    
Auto-generated code properly works and will dispose stuff added in the designer file. You typically only have to dispose stuff that you create yourself.

1 solution

There is a blog post at below link discussing similar issue:
http://blogs.msdn.com/b/ploeh/archive/2006/08/10/howtodisposemembersfromforms.aspx[^]
It describes how to dispose Members From Forms. In the comments section of that post Author has provided below code with explanation.

C#
public partial class MyForm : Form
{
    private MyDisposable myDisposable_;

    public MyForm()
    {
        InitializeComponent();

        this.myDisposable_ =
            new MyDisposable("Goodbye, World");

            this.Disposed += new EventHandler(this.OnMyFormDisposed);
    }

    void OnMyFormDisposed(object sender, EventArgs e)
    {
        this.myDisposable_.Dispose();
    }
}


further below links would be helpful:

http://stackoverflow.com/questions/1052147/how-do-i-extend-a-winforms-dispose-method[^]

http://blogs.msdn.com/b/jfoscoding/archive/2005/08/12/450835.aspx[^]

Thanks.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900