Re: A re-announce on GC's defects
- From: Barry Kelly <barry.j.kelly@xxxxxxxxx>
- Date: Fri, 19 Jan 2007 00:14:25 +0000
Michael D. Ober wrote:
You never have to call the IDispose interface. The GC will do this for you.
The GC never calls Dispose(). It makes its best effort to call
Finalize() as soon as possible if it's been overridden, but very few
disposable (implementing IDisposable) objects should have finalizers
(which should be restricted to handle-like objects).
The implementing Finalize() ("~ClassNameHere()" in C#) according to
recommended practices involves writing a protected virtual Dispose(bool)
method, but that's a separate (yet related) issue to implementing
IDisposable.
Do not confuse IDisposable with Finalize(). Any class overriding
Finalize() should implement IDisposable, but the reverse is *not* true.
Objects should implement IDisposable when they override Finalize, or if
they are the logical owner of any other resource which implements
IDisposable.
And ignore the horrific example of System.ComponentModel.Component. That
class is like a car accident :). You should implement Finalize() only on
handle-like classes, or preferably descend from SafeHandle (or one of
its descendants) and override the appropriate methods.
The runtime will eventually get around to releasing
those resources via the object's IDispose interface - you don't have to do
it yourself.
You've got the .NET GC wrong, I'm afraid. It knows nothing about
IDispose, or the Dispose() method. And it certainly tries hard to call
finalizers on finalizable objects (objects overriding the Finalize()
method) as soon as possible, but leaving resource deallocation up to the
GC is irresponsible and a recipe for bugs. It will leave sockets open,
files open and locked, database connections open etc. longer than
necessary, and will ultimately result in unexpected resource acquisition
failures.
For an easy life, deterministically dispose your resources, please!
In C#, the runtime will execute
the destruction code for you when it either has idle time or needs the
memory to satisfy a allocation request.
..NET doesn't have an exact analogue to C++ destruction. C++ destruction
combines two things: (1) memory disposal and (2) resource disposal. In
..NET, the GC does (1) and has facilities to catch (2) as a *last* line
of defense, while IDisposable is for (2).
You never need to call Dispose. By telling the compiler that an object
class implements IDisposable, the GC will call Dispose for you before it
actually deallocates memory.
Please, educate yourself before you spread more misinformation!
-- Barry
--
http://barrkel.blogspot.com/
.
- Follow-Ups:
- Re: A re-announce on GC's defects
- From: Bruce Wood
- Re: A re-announce on GC's defects
- References:
- A re-announce on GC's defects
- From: Born
- Re: A re-announce on GC's defects
- From: Andre Kaufmann
- Re: A re-announce on GC's defects
- From: Michael D. Ober
- A re-announce on GC's defects
- Prev by Date: MS's official variable/function naming standard
- Next by Date: Re: Mouse events
- Previous by thread: Re: A re-announce on GC's defects
- Next by thread: Re: A re-announce on GC's defects
- Index(es):
Relevant Pages
|