Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

COM object that has been separated from its underlying RCW cannot be used

Tags:

interop

This can happen for a few reasons, the big ones I know of are below.

Event Handlers Without Strong References to the Delegate

A caller subscribes to an event on the com object without keeping a strong reference to the callback delegate. Here is an example of how to do this correctly and how to not do it: The reason for this is a strong reference needs to be kept to the delegate, if it goes out of scope, the wrapper will release the reference count for the interface and bad things will happen.

public class SomeClass
{
    private Interop.ComObjectWrapper comObject;
    private event ComEventHandler comEventHandler;

    public SomeClass()
    {
        comObject = new Interop.ComObjectWrapper();

        // NO - BAD!
        comObject.SomeEvent += new ComEventHandler(EventCallback);

        // YES - GOOD!
        comEventHandler = new ComEventHandler(EventCallback);
        comObject.SomeEvent += comEventHandler
    }

    public void EventCallback()
    {
        // DO WORK
    }
}

Calls to a disposed Runtime Callable Wrapper

The wrapper has been disposed and calls are being made after it has been disposed. A common way this can happen is if a control is using an activex control or COM object and the controls Dispose() is called out of order.

  • A form gets Close() called.
  • System.Windows.Forms.Close() will call Dispose()
  • Your forms virtual Dispose() will be called which hopefully calls base.Dispose() somewhere. Systems.Windows.Forms.Dispose() will release all COM objects and event syncs on the form, even from child controls.
  • If the control that owns a com object is explicitly disposed after base.Dispose() and if it calls any methods on it's COM object, these will now fail and you will get the error “COM object that has been separated from its underlying RCW cannot be used”.

Debugging Steps

A good way to debug this issue is to do the following:

  1. Write a class that inherits from the Interop class (otherwise known as the runtime callable wrapper or RCW).
  2. Override DetachEventSink
  3. Override Dispose
  4. Call your new class instead of calling the interop class directly
  5. Add breakpoint to DetachEventSink and Dispose
  6. See who is calling these methods out of order

One other thing

This isn't related to this issue but while we are on the topic, unless you know otherwise, always remember to check that the thread your COM objects are being used from are marked STA. You can do this by breaking in the debugger and checking the value returned from:

Thread.CurrentThread.GetApartmentState();

It's somewhat hard to tell what your actual application is doing, but it sounds like you may be instantiating the COM object and then attempting to access it from another thread, perhaps in a Timer.Elapsed event. If your application is multithreaded, you need to instantiate the COM object within each thread you will be using it in.