Dispose a managed object

G

Gravy

Hi there,

can someone clarify something for me please? I have a windows form class
that contains a Font private member which is initialised (new'ed) in the
forms constructor. The font is used in formatting a richtextbox control.
Do I have to dispose of the font member in the forms Dispose method or does
this happen automatically because it is a member of the form?

I think I need to call font.Dispose on it but I wanted some confirmation on
this first. If I do have to call Dispose on it should I do this in the
form's Dispose method?

Regards

Graham Allwood
 
H

Herfried K. Wagner [MVP]

* "Gravy said:
can someone clarify something for me please? I have a windows form class
that contains a Font private member which is initialised (new'ed) in the
forms constructor. The font is used in formatting a richtextbox control.
Do I have to dispose of the font member in the forms Dispose method or does
this happen automatically because it is a member of the form?

I think I need to call font.Dispose on it but I wanted some confirmation on
this first. If I do have to call Dispose on it should I do this in the
form's Dispose method?

The call to the 'Dispose' method is not mandatory. If the GC is about
to destoy the object, it will be disposed automatically.
 
G

Gravy

So calling Dispose is like being a good citizen, it's not mandatory but it
does free resources ASAP?

Graham
 
H

Herfried K. Wagner [MVP]

* "Gravy said:
So calling Dispose is like being a good citizen, it's not mandatory but it
does free resources ASAP?

That's true. By calling 'Dispose', unmanaged resources that are held by
the object are released, for example file handles, database connections,
GDI objects, ... These objects would not be freed until the GC destroys
the object. If, for example, the number of GDI handles is limited,
calling 'Dispose' to internally free the handles that are not needed any
more, will result in higher scalability.
 
J

Jediah L.

Just a fair warning that in some cases not calling Dispose() on unmanaged
resources can have detrimental impact on your application (for example on
database connections, or components derived from System.EnterpriseServices).

That being said - I would agree - not disposing your Font is probably not
going to have a big impact on your application, especially if in your
scenario the Font wouldn't be disposed until the form is closed.




For more information on Dispose, see the following MSDN Articles:


http://msdn.microsoft.com/library/d...guide/html/cpconimplementingdisposemethod.asp

http://msdn.microsoft.com/library/d.../en-us/cpgenref/html/cpconFinalizeDispose.asp
 
C

Chris Lyon [MSFT]

The call to the 'Dispose' method is not mandatory. If the GC is about
to destoy the object, it will be disposed automatically.

This statement is a bit misleading. The GC *never* calls Dispose on an object. The GC will call an object's finalize method (which should, in turn, call Dispose). Note, this does
not happen when the GC is "about to destroy an object". When an object is no longer referenced, it is considered garbage. The GC will reclaim that memory the next time it
performs a collection, unless the object has a finalizer, in which case, it is put on the finalization queue, and its finalizer run on a seperate thread at an undetermined time in the
future. After the finalizer is run, assuming the object has not been resurrected, its memory is reclaimed. The GC has no knowledge of unmanged resources (file handles, GDI
handles, socket connections, etc), so they must be explicitly released in a Dispose method, and in a finalizer as a fall-back (see the Dispose pattern links further down this thread).

What that means for you, if you have a object that implements a Dispose method, call it as soon as you're finished with the object. This will release the unmanaged resources
sooner than waiting for the finalizer to be run.

Hope that helps
-Chris


--

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.
 
G

Gravy

Out of interest, if I use these Disposable types in a windows form, can (or
should) I add them to the forms components collection as that automatically
has its contents disposed of in the forms own Dispose method?

Graham

"Chris Lyon [MSFT]" said:
The call to the 'Dispose' method is not mandatory. If the GC is about
to destoy the object, it will be disposed automatically.

This statement is a bit misleading. The GC *never* calls Dispose on an
object. The GC will call an object's finalize method (which should, in
turn, call Dispose). Note, this does
not happen when the GC is "about to destroy an object". When an object is
no longer referenced, it is considered garbage. The GC will reclaim that
memory the next time it
performs a collection, unless the object has a finalizer, in which case,
it is put on the finalization queue, and its finalizer run on a seperate
thread at an undetermined time in the
future. After the finalizer is run, assuming the object has not been
resurrected, its memory is reclaimed. The GC has no knowledge of unmanged
resources (file handles, GDI
handles, socket connections, etc), so they must be explicitly released in
a Dispose method, and in a finalizer as a fall-back (see the Dispose
pattern links further down this thread).

What that means for you, if you have a object that implements a Dispose
method, call it as soon as you're finished with the object. This will
release the unmanaged resources
sooner than waiting for the finalizer to be run.

Hope that helps
-Chris


--

This posting is provided "AS IS" with no warranties, and confers no
rights. Use of included script samples are subject to the terms specified
at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
 

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