Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / August 2004

Tip: Looking for answers? Try searching our database.

Dispose a managed object

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gravy - 27 Aug 2004 16:17 GMT
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
Herfried K. Wagner [MVP] - 27 Aug 2004 18:13 GMT
* "Gravy" <Gravy@discussions.microsoft.com> scripsit:
> 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
[quoted text clipped - 5 lines]
> 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.

Signature

M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/

Gravy - 27 Aug 2004 20:54 GMT
So calling Dispose is like being a good citizen, it's not mandatory but it
does free resources ASAP?

Graham

>* "Gravy" <Gravy@discussions.microsoft.com> scripsit:
>> can someone clarify something for me please? I have a windows form class
[quoted text clipped - 11 lines]
> The call to the 'Dispose' method is not mandatory.  If the GC is about
> to destoy the object, it will be disposed automatically.
Herfried K. Wagner [MVP] - 27 Aug 2004 23:12 GMT
* "Gravy" <Gravy@discussions.microsoft.com> scripsit:
> 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.

Signature

M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/

Jediah L. - 28 Aug 2004 02:28 GMT
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/default.asp?url=/library/en-us/cpguide/html/cp
conimplementingdisposemethod.asp


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/c
pconFinalizeDispose.asp


>* "Gravy" <Gravy@discussions.microsoft.com> scripsit:
>> So calling Dispose is like being a good citizen, it's not mandatory but
[quoted text clipped - 7 lines]
> calling 'Dispose' to internally free the handles that are not needed any
> more, will result in higher scalability.
"Chris Lyon [MSFT]" - 30 Aug 2004 17:17 GMT
>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

Signature

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.

Gravy - 31 Aug 2004 16:33 GMT
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

> >The call to the 'Dispose' method is not mandatory.  If the GC is about
>>to destoy the object, it will be disposed automatically.
[quoted text clipped - 22 lines]
> Hope that helps
> -Chris

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.