What do I need to clean up in dispose?

K

Kevin Peck

We have various user controls and forms that will be loaded and unloaded
during a program run. I want to be sure we are not going to leak memory.

In the InitializeComponent method a lot of events are being added via +=
calls. Do I need to clean any of these up?

We create basic fonts as member variables. Do I need to dispose of them?

We create pens and brushes as member variables. Do I need to dispose of
them?

I assume that any += delegate not in the InitializeComponent is up to me to
clean up.
 
I

Ian Cooper

We have various user controls and forms that will be loaded and unloaded
during a program run. I want to be sure we are not going to leak memory.
The garbage collector should handle memory management for you - the dispose pattern is about ensuring the release of handles to system resources such as windows or files.
In the InitializeComponent method a lot of events are being added via +=
calls. Do I need to clean any of these up?
No. Event handlers will be cleaned up by GC.
We create basic fonts as member variables. Do I need to dispose of them?
We create pens and brushes as member variables. Do I need to dispose of
them?
It used to be that GDI resources were scarce and you needed to clean them up as soon as you could after using them, but it does not seem to be as significant a problem under GDI+ . To be efficient you can dispose of them after use, or if they are members in your form.
I assume that any += delegate not in the InitializeComponent is up to me to clean up.
Just let the GC do it for you

Ian Cooper
wwww.dnug.org.uk
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top