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?