
Signature
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/
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.
>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