Trey,

Sorry for my extended rants, looks like I misunderstood your posts -- there
are many misconceptions about the role of C# destructors / finalizers and
IDispose floating about, so that's what I was trying to clear up. But since
you already know what you're doing I'm afraid I can't offer much help -- as
I said I'm personally quite happy with the clean-up facilities provided by
the .NET Framework and the C# language.

I do admit that the C# using statement is one big reason why I'm happy with
this situation, and I agree that other .NET languages including Managed C++
would greatly benefit from the addition of such a statement.

>This raises an interesting question about the GC implementation.  Since it
>knows of all of the roots' existence, how hard/inefficient would it be for
>the GC to automagically call Dispose() on a type that implements
>IDisposable if it absolutely knew that the last known root to that type
>just went out of scope?  I imagine this would require a significant amount
>of work to make it efficient compared to the current GC implementation.

Well, the GC actually does call Dispose on objects as they are destroyed,
namely through the finalizer if it's written correctly. But that happens
non-deterministically as usual, at some arbitrary time after the last
reference was dropped.

Your suggestion would mean that the GC had to be reworked to finalize
IDisposable objects deterministically. With the present implementation,
that would mean invoking the reference checker whenever an IDisposable
object goes out of scope, and then proceeding with garbage collection if
there are no more references. That would mean starting the GC whenever e.g.
a method returns that has local IDisposable references. This sounds like an
unacceptable performance degradation to me, though it might be possible to
design separate GC algorithms specifically for these objects.

Cheers, Chris

You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced 
DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to