I am gonna risk posting this here so the super intelligent folks on this
newsgroup can fix my understanding of the picture.
.. I don't mind
making myself look like a complete idiot if I learn something good out of
this.
So here's my understanding ----
Think of it this way, you are writing a class that holds a bunch of
expensive objects.
Lets say your class is "C" and the expensive objects it holds are A and B
Now you know that your objects are expensive, and being the prudent
developer you are, you decide to implement IDisposable, which means add a
method called "Dispose".
Now in your code say in function function "f" calls function "F" you do
this.
f{
F() ;
// Point #Y
}
F
{
... working with C
C.Dispose
{{
// Calling C.Dispose ended up calling ...
A.Dispose() ;
B = null ;
}}
/// Point #X
}
For argument's sake lets assume that A was a managed object that
implemented Dispose, and B was say an instance of a COM object. ... then
...
At Point #X - A and B will be GC-reusable, because A fell out of scope and
B was explicitly set to null, but C will not be.
But at Point #Y - C will be GC-reusable.
In other words, Dispose is just as special as your implementer implemented
it. And yes by convention if a class implements it, you should call it.
In that sense, calling dispose on a Connection object, does NOT Guarantee
reuse of GC memory by the object itself, but it does guarantee GC reuse of
the expensive objects it might hold, and the ConnectionPooling Mechanism
in ADO.NET underneath ensures that a closed connection (closed by say
SqlConnection.Close()) is happily reusable. So when Connection.Dispose
calls Connection.Close, the exact pointer reference of the connection
object still stays unusable (which is no big deal) until the connection
object falls out of scope or is explicitly set to null, but the underlying
Connection to the database is pooled.
And as far as GC goes, it calls Finalize, which calls it in a hurry during
GC and might not do as good a cleanup job as your code might ask it to do
in Finalize. Plus in Dispose you should have called SuppressFinalize
anyway. I usually call a Debug.Print also, to catch wherever in my code
someone forgot to call a dispose where he shud've.
Am I right or am I high on Advil? (WHEEZE COUGH COUGH !!)