"Jared Parsons [MSFT]" <ja******@online.microsoft.com> escribió en el
mensaje news:%2***************@TK2MSFTNGP15.phx.gbl...
Small correction. I believe that it should be "protected virtual void
Dispose(bool disposing)".
Yes, of course, I just typed that too fast.
This pattern solves the problems that occur because your objects can be
disposed in two different ways. The first is explicitly and occurs when
you call Dispose() on an object or create it via a using statement. The
second is when the garbage collector finds it.
In the first case, you are disposing the object yourself so you can have a
reasonable degree of confidence in the state of your managed objects.
Thus you can dispose them as well (if necessary).
In the case that the garbage collector disposes your objects, you do not
know there state. It is possible that the garbage collector disposed a
object to which you have a reference just before it disposed the parent
object. In that case you should only free up your unmanaged objects.
I understand why you can only dispose of dependent managed objects inside
the
if ( disposing )
...
branch.
My question however is about the term "free an object" and its relation with
Dispose.
To me, and I'm pretty sure that to most anyone else, to "free an object"
means to reclaim the memory it uses.
But Dispose has nothing to do with (managed) memory.
Brad says earlier in his talk that "you use Dispose to a free an object",
but that's incorrect. Unless I'm totally missing something, Dispose has
nothing to do with freeing (i.e. reclaiming memory); it has to do with
releasing (notice the different term) unmanaged resources.
I truly believe that becasue of sources like Brad's talk many people think
Dispose() is about deterministcally destroying (thus freeing) an object. It
is not.
Calling Dispose() doesn't free the object at all; it just allows the object
to release any unmanaged resources it could be using. The managed memory
block used by the object (and all its dependent objects) will still be
allocated after the call to Dispose().
Please don't get me wrong: of course nor Brad nor you erroneusly think
Dispose() reclaimns managed memory, it's obvious you both know that well. My
point is that this stuff is wrongly explained because of a bad choice of
wording: Dispose() doesn't free up any managed objects, that's the job of
the GC and you just can't do that manually. Dispose() is ONLY about
releasing resources don't managed by the GC (thus called unmanaged
resources)
Fernando Cacciola
SciSoft