views:

22

answers:

2

I've exposed a .net class to COM. The class inherits from IDisposable because I need to clean up some unmanaged resources. In the .net environment I would wrap my class in a using scope to ensure that Dispose() always gets called. Is there some facility to do this if I'm using the COM wrapper? I'm insantiating the class from VB6 and I'd like to ensure Dispose gets called, or should I just trust it will get called eventually by the CLR

A: 

Dispose will not be called automatically, but a finalizer will. If you have unmanaged resources than need cleaning up, implement a finalizer that calls Dispose(). It's common practice to implement another method, Dispose(bool disposing) which is called with true from IDisposable.Dispose() and false from your finalizer. If disposing is true, you should clean up any managed resources (if needed) and call GC.SuppressFinalize(this).

Bob
A: 

My understanding (possibly quite rusty) is that the COM Callable Wrapper (CCW) doesn't call Dispose() when the COM object's refcount drops to 0 - it simply no longer references the CLR object.

You'll need to require that users of the object explicitly call Dispose() and rely on a finalizer to do the clean up if they don't.

Some other ideas that may help here:

Michael Burr