Hi all, I originally posted this to DOTNET-CLR and meant to voice the same ideas here. I hope this issue is not an old one, but here goes.
Up front, I was wondering if the C++ managed extensions package provides anything such as the C# 'using' statement to make use of the IDispose easier. Something tells me that the answer is, "no". For some reason, it seems to me that it would have been much more natural for MS to wire the C# destructor to the IDispose interface rather than the Finalize() method. I understand there could be some implementation details in there that would make that difficult, but it does seem more natural. Of course, there are garbage collection issues and then you're back to the whole refcounting madness that we had back in the COM days. For example, it would not be good for an object to be disposed when it leaves a scope while another scope is using it. But then again, you can get into this same boat using the IDispose pattern with ref type objects anyway. There are some issues here that need to be addressed. This stuff is not easy to solve. For example, look how long it took for exception safety concepts to mature in the C++ world. For a good example of what I mean, read Sutter's "Exceptional C++". When exceptions first came out in C++, many were under the assumption that exception safety was all about try/catch blocks. This is definitely not the case. Moreover, who else agrees that __finally blocks are ugly and extremely error prone and difficult to maintain? What sort of solution could we use to allow what C++ developers know as a destructor to do automatic cleanup? I understand C#'s 'using' statement makes life easier in this regard. I'm assuming that if an exception propagates from within one of those blocks that it will handle the IDispose the same way as if it left the 'using' block gracefully. I believe that much more light needs to be shed on the issues surrounding the proper time to use a ref type as opposed to a value type. With regards to exception safety, I think value types win hands down. But, I've also heard that value types in the .NET framework should be kept below a certain footprint size. By exception safety I mean, the state of the system is not left in a half baked state when an exception occurs. Destructors (and IDispose) are essential in this regard, unless you want to litter your code with unmanageable, error prone __finally blocks. Within the confines of C++, and using managed objects, d'tors don't behave the way they used to. So, in order to trigger the dispose pattern, it seems that a stack based native C++ class is useful for handling this situation, no? Am I smoking something, or does anyone else agree that there are some very open ended issues here. :-) I can see issues like this popping up in a future book that could be called, say, "Effective .NET". :-)) Thanks, -Trey You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.
