I understand the idea behind memory management of the GC. What I don't
understand is why .NET classes cannot clean up their unmanaged resources
themselves when they are collected through their destructor methods (or
finalizers, whatever they are called). Classes in C++ implement destructors
to do cleanup when they go out of scope. The user of any objects
instantiated from that class do not have to explicitly call a cleanup
methods of that object. The object does cleanup on its own when it is
destroyed.
Thus, I don't understand why .NET classes that require cleanup of unmanaged
resources need an IDisposable interface that the user must explicitly call
before the object is garbage collected. Why not just put it in a destructor
or finalize and have the GC call it? Maybe this is already done, but I
don't understand why there is always an emphasis to call Dispose() on
objects such as a SQLConnection(), when it should do it on its own when it
is finalized.
--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.
EmailID = varnk
Domain = Diebold.com
-----------------------------------
"Chris Lyon [MSFT]" said:
Hi Ken
NET's Garbage Collector, like you've stated, automated memory management,
relieving the developer of the need to explicitly delete objects. What the
GC does not do (nor
has it ever claimed to do) is clean up unmanaged resources (database
collections, file handles, etc). The developer must explicitly clean up
these resources, usually through
the Dispose pattern. Note, calling Dispose is only necessary if an
object holds unmanaged resources.
Hope that makes sense
-Chris
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.