Using statement around dialog form to ensure garbage collection

10,225

Solution 1

According to MSDN, you must explicitly call Dispose on forms shown using ShowDialog (unlike with the Show method):

When a form is displayed as a modal dialog box, clicking the Close button (the button with an X at the upper-right corner of the form) causes the form to be hidden and the DialogResult property to be set to DialogResult.Cancel. Unlike non-modal forms, the Close method is not called by the .NET Framework when the user clicks the close form button of a dialog box or sets the value of the DialogResult property. Instead the form is hidden and can be shown again without creating a new instance of the dialog box. Because a form displayed as a dialog box is hidden instead of closed, you must call the Dispose method of the form when the form is no longer needed by your application.

Solution 2

For modal dialogs, you should use the pattern:

using ( var dlg = new MyDialog() )
{
    // other code here to initialize, etc.
    dlg.ShowDialog();
}

Since MyDialog is derived from Form, and Form implements IDisposable, this pattern will correctly cleanup your dialog.

This should not be a "short term workaround", but the standard way you should invoke all your modal dialogs.

Modeless dialogs are another story. You would need to keep track of them yourself, and call Dispose at the appropriate points in your application.

Solution 3

In general it is a good way to use the using statement for objects that implement IDisposable.

A little sample situation:

Lets say you have a third party "component". You cant know what goes on internally and maybe this objects creates a temp file and delete the file on Dispose. If you dont call Dispose() and dont use using the file will never get deleted.

In your case you can do it too as long as you open your form modal.

Share:
10,225
Avalanchis
Author by

Avalanchis

I'm a senior .NET developer who also enjoys working with iOS and DevOps. I'm an AWS Certified Developer and a Certified ScrumMaster.

Updated on June 20, 2022

Comments

  • Avalanchis
    Avalanchis almost 2 years

    We have a Windows Forms application that contains thousands of forms.

    Many of these are temporarily displayed as dialogs via the ShowDialog() method.

    This application has been around for years and we've discovered that many of the forms are not getting garbage collected in a timely manner due to various resource leaks in the form or the controls that it uses.

    Specifically, we've found examples of GDI+ resources that aren't being disposed of properly, although there may be other types of resource leaks that have not yet been characterized.

    Although the right way to resolve this is obviously to go through every form and every control and eliminate all of the resource problems. This will take some time to accomplish.

    As an short term alternative, we have found that explicitly calling Dispose() on the form seems to initiate the garbage collection process and the form and its resources are deallocated immediately.

    My question is whether is would be a reasonable workaround to wrap each form's ShowDialog() block in a using statement so that Dispose() is called after the form has been displayed, and also would this be a good practice to institute in general?

    For example, change the existing code from this:

    public void ShowMyForm()
    {
        MyForm myForm = new MyForm();
        myForm.ShowDialog();
    }
    

    To this:

    public void ShowMyForm()
    {
        using (MyForm myForm = new MyForm())
        {
            myForm.ShowDialog();
        }
    }
    

    In our testing, MyForm's Dispose() method never gets called for the first example, but it gets called immediately for the second example.

    Does this seem like a reasonable approach as a short term workaround while we spend the time tracking down each of the specific resource issues?

    Are there other approaches that we could consider for a short term workaround and/or methodologies for identifying and resolving these types of resource issues?

  • Avalanchis
    Avalanchis over 12 years
    This was very interesting to learn, especially since it appears right in the help text for the Form.ShowDialog method: msdn.microsoft.com/en-us/library/c7ykbedk.aspx
  • Avalanchis
    Avalanchis over 12 years
    If Dispose is not called after ShowDialog, but there are no resource leaks in the form, will it eventually get disposed of anyway (prior to the app terminating)?