Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Always dispose a form

4.43/5 (9 votes)
23 Nov 2009CPOL 16.1K  
Some time ago, we noticed that in one of our projects the size of memory of our application is growing and it never decreased.After some checking and testing we realized that even if we have a local reference to a form, if we haven't  disposed it explicitly it will remain on the stack and never

Some time ago, we noticed that in one of our projects the size of memory of our application is growing and it never decreased.

After some checking and testing we realized that even if we have a local reference to a form, if we haven't  disposed it explicitly it will remain on the stack and never be collected.  

So now, instead of writing this:

C#
new usersDialog().ShowDialog()

We write: 

C#
using(var dlg=new usersDialog())
    dlg.ShowDialog();

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)