Click here to Skip to main content
15,892,480 members
Articles / Programming Languages / C#
Tip/Trick

Always dispose a form

Rate me:
Please Sign up or sign in to vote.
4.43/5 (9 votes)
23 Nov 2009CPOL 15.8K   9   4
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)


Written By
Web Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralForms Should Be Garbage Collected Fine Pin
AspDotNetDev18-Nov-09 15:42
protectorAspDotNetDev18-Nov-09 15:42 
GeneralRe: Forms Should Be Garbage Collected Fine Pin
supercat919-Nov-09 9:13
supercat919-Nov-09 9:13 
GeneralGood point, but typo in headline. Pin
supercat918-Nov-09 6:01
supercat918-Nov-09 6:01 
The content also seems a little light to be worth an article, though the point you make is certainly well-taken.

Actually, I think my preference is to have a form which is meant to be used as a dialog have a static/shared procedure which will take care of everything.
GeneralRe: Good point, but typo in headline. Pin
AspDotNetDev18-Nov-09 15:18
protectorAspDotNetDev18-Nov-09 15:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.