Dispose managed resources

G

gol

Hi,
When I create a Windows Form in VS 2005 I get the Dispose method created for
me in the Form.Designer.cs file. Some of the code that I get inside that
method takes care of cleaning up managed resources this way:

if (disposing && (components != null))
{
components.Dispose();
}

The question is whether this is enough to clean up all managed resources, or
I should add some code myself to clean up managed resources that I use.

Is System.Windows.Forms.Timer considered to be a managed resource? How is
this being cleaned up?

Thank you very much
 
B

Brian Gideon

Hi,
When I create a Windows Form in VS 2005 I get the Dispose method created for
me in the Form.Designer.cs file. Some of the code that I get inside that
method takes care of cleaning up managed resources this way:

if (disposing && (components != null))
{
components.Dispose();

}

The question is whether this is enough to clean up all managed resources,or
I should add some code myself to clean up managed resources that I use.

Is System.Windows.Forms.Timer considered to be a managed resource? How is
this being cleaned up?

Thank you very much

If your Form contains a reference to a object that implements
IDisposable then you should call Dispose on that object when the
Form's Dispose method is called.

Likewise, if your Form maintains an unmanaged resource then you need
to release that resource in the Form's Dispose method.

One thing to consider, however, is whether it makes more since to do
these in response to the Form closing.
 

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