Using a dialog expression to ensure garbage collection

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

Many of them are temporarily displayed as dialogs using the ShowDialog () method.

This application has existed for many years, and we found that many forms do not receive garbage collection in a timely manner due to various leaks of resources in the form or controls that it uses.

In particular, we found examples of GDI + resources that are not disposed of properly, although there may be other types of resource leaks that have not yet been characterized.

While the right way to resolve this is to obviously go through every form and every control and fix all resource problems. It will take some time.

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

My question is, would it be a sensible workaround to wrap each block of the ShowDialog () form in the using statement so that Dispose () is called after the form is displayed, and would that also be good practice for embedding in general?

For example, modify the existing code as follows:

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

For this:

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

In our testing, the MyForm Dispose () method is never called for the first example, but it is invoked for the second example.

Does this seem like a smart approach as a short-term workaround while we spend time tracking each specific resource problem?

Are there other approaches that we could consider for a short-term workaround and / or methodologies for identifying and solving these resource problems?

+7
source share
3 answers

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

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

+11
source

For modal dialogs you should use a template:

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

Since MyDialog is derived from Form , and Form implements IDisposable , this template will properly clear your dialog.

This should not be a β€œshort-term workaround,” but in a standard way you should call all your modal dialogs.

Modeling dialogs is another story. You will need to monitor them yourself and call Dispose at the appropriate points in your application.

+2
source

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

A small situation with the sample:

Suppose you have a third-party component. You cannot know what is going on inside and perhaps these objects create a temporary file and delete the file on Dispose . unless you call Dispose() and use using , the file will never be deleted.

In your case, you can do this while you open the modal form.

+1
source

All Articles