Are dynamic COM objects managed resources?

I have a dynamic COM object as a private field in my class. I'm not sure if this is considered a managed resource (the GC clears it) or not.

 private dynamic _comConnector = null; 

...

 _comConnector = Activator.CreateInstance(Type.GetTypeFromProgID("SomeProgId")); 

When implementing IDispose, should it be cleared as a managed resource (only if Dispose () is explicitly called) or as its own resource (when Dispose (false) is also called from the finalizer)?

 private void Dispose(bool disposing) { if (disposing) { // Free managed resources // // --> Should I call Marshal.FinalReleaseComObject(_comConnector) here? } // Free unmanaged resources // // --> Or maybe here? } 
+7
source share
2 answers

This is a managed resource (mainly a Runtime Callable Wrapper), and you should clear it as such. MSDN declares that :

Each RCW maintains an interface pointer cache on a COM object, which it wraps and releases a reference to a COM object when RCW is no longer required. The runtime collects garbage on the RCW.

those. RCW is a managed resource that wraps unmanaged COM links.

As in other cases, freeing COM objects can be dangerous if you use them from multiple threads in multiple places in your application, as described in this blog post from Chris Brumme .

If you are using a single-threaded COM object, you can safely call ReleaseComObject on that object when you are done with it: I hope this is your case.

+6
source

1 COM is a component, so it all depends on what it does. if it performs the following functions

  • Open files

  • Open network connections

  • Unmanaged memory

  • In XNA: vertex buffers, index buffers, textures, etc.

=> So yes, this is not a managed ressource

The garbage collector has no information about this resource, so it is for the user

2 For clean, you can simply use the Dispose method (or use the block that calls dispose at the end of treatment).

0
source

All Articles