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

kevin marchand picture kevin marchand · Oct 14, 2009 · Viewed 72.4k times · Source

I am trying to use the OpcRcw.da.dll. If I interop this dll inside a test console project everything works, but if I build dll project to do my interop gymnastic and ref my library into my console project I am getting this error:

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

What need to be done to a class lib project to not kill the RCW ref?

Answer

Steve Sheldon picture Steve Sheldon · Apr 15, 2011

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();