C# - 4 - Winform Dispose() gotcha.

[C# 2.0]

While still on the subject of disposing and memory leaks here is a little gem:

A winform has a Dispose() method (which is autogenerated in the designer.cs file). If that form is called with a Show() then when closed it will automaticaly call it's Dispose() method.


If that form is instead called with a ShowDialog() then it's Dispose() method is NOT automatically called.

So, after every ShowDialog() you should explicitly call that form's Dispose() method.


(Apparently, an MDI child form that is not visible will also not automatically call it's Dispose() method on closing. But this has yet to be verified by this author.)


Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License