Calling Dispose() after ShowDialog()

  • Thread starter Thread starter Steve Austin
  • Start date Start date
S

Steve Austin

Is this absolutely necessary to avoid memory leaks? If so, would:

using( MyForm form = new MyForm() )
{
form.ShowDialog();
}

be acceptable?

Steve
 
Hi Steve,

Yes it is completely fine if you do all dialog box's field reading inside
the using block.

Just a little correction. You won't have memory leaks even if you don't call
dispose. As long as you don't keep references to the dialog box it will be
garbage collected and disposed as soon as there is need of memory. But
because the dialog box holds unamaged resources it is good practice to call
dispose ASAP when you finish using it. Bare in mind that GC doesn't monitor
unmanaged resources and GC will not collect the garabage if you run out of
unmanaged resources or memory. In the next version of the framework GC
introduces one new method (AddMemoryPressure) that is kind of workaround
those problems
 
* "Steve Austin said:
Is this absolutely necessary to avoid memory leaks? If so, would:

using( MyForm form = new MyForm() )
{
form.ShowDialog();
}

be acceptable?

In addition to the other post: Call 'Dispose' for forms shown using
'ShowDialog', don't call it for forms shown using 'Show'. These forms
will be disposed automatically when they are closed.
 
Thanks for clarifying that fellas :)

Herfried K. Wagner said:
In addition to the other post: Call 'Dispose' for forms shown using
'ShowDialog', don't call it for forms shown using 'Show'. These forms
will be disposed automatically when they are closed.
 
Back
Top