title: Using statement around dialog form to ensure garbage collection
body: <p>We have a Windows Forms application that contains thousands of forms.</p>
  
  <p>Many of these are temporarily displayed as dialogs via the ShowDialog() method.</p>
  
  <p>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.  </p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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?</p>
  
  <p>For example, change the existing code from this:</p>
  
  <pre><code>public void ShowMyForm()
  {
      MyForm myForm = new MyForm();
      myForm.ShowDialog();
  }
  </code></pre>
  
  <p>To this:</p>
  
  <pre><code>public void ShowMyForm()
  {
      using (MyForm myForm = new MyForm())
      {
          myForm.ShowDialog();
      }
  }
  </code></pre>
  
  <p>In our testing, MyForm's Dispose() method never gets called for the first example, but it gets called immediately for the second example.</p>
  
  <p>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?</p>
  
  <p>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?</p>