System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW...

Hi,

i'm doing some test using the .NET 2.0 TestTools and Visual Studio 2005. The code below is pseudo code just to demonstrate what i'm doing. Im testing a singleton class that creates an COM Instance somewhere during construction/initialization, so the classes state will be keep the same in all tests. The Problem is, each test runs for itself, running all test will give a strange result - the first test succeeds, all other fail (see below, Scenario A). Wrapping the 'tests' in a single test method (see below, Scenario B) will run all former test methods (not longer marked with the [TestMethod] attrib) with success.

Scenario A always leads to the following exception: "System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used.." - result of a corrupted COM reference (see myComInstance below).

Anyone experienced the same problem with corrupted COM references during unit testing Have found a thread in the J# forums where this exception is discussed, but the solution doesn't fit to my problem. My thought is, that in vs2005 each testmethod runs in it's own thread and therefore the COM ref gets corrupted.

Not quite sure if i explained the problem correctly (my english has got worse over the last couple of years).

thanx, mm

p.s. here's the pseudo code

// >> Class to test -------------------------------

public class MyClass : IDisposable

{

public static MyClass Instance = new MyClass();

private ThirdPartySDK.ServerClass _myComInstance = null;

private MyClass()

{

// Singleton

_myComInstance = new ThirdPartySDK.ServerClass();

}

public void DoSomeThing()

{

_myComInstance.DoSomeThing("important stuff");

}

public void DoSomeOtherThing()

{

_myComInstance.DoSomeOtherThing("more important stuff");

}

public void Dispose()

{

// the stdandard dispose stuff, creating

// an Dispose(bool) and releasing

// the COM Ref via Marshal.ReleaseComObject(_myComInstance);

}

}

// >> Tests (Scenario A) -------------------------------

[TestMethod]

public void Test_DoSomeThing()

{

MyClass.Instance.DoSomeThing();

}

[TestMethod]

public void Test_DoSomeOtherThing()

{

MyClass.Instance.DoSomeOtherThing();

}

// >> Tests (Scenario B) -------------------------------

[TestMethod]

public void WrappedTests()

{

Test_DoSomeThing();

Test_DoSomeOtherThing();

}

public void Test_DoSomeThing()

{

MyClass.Instance.DoSomeThing();

}

public void Test_DoSomeOtherThing()

{

MyClass.Instance.DoSomeOtherThing();

}




Answer this question

System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW...

  • Pak Lui

    There's a hot fix available from MSFT for this issue. Seems pretty specific to ASP.NET though...


  • jpcesar

    I am also getting this same error in similiar testing scenerio. Ours didn't have any clean up though (I don't know why - it should've) so I was hoping that was the problem, but by your example this doesn't seem to make a difference and after implementing clean up I'm getting the same thing still. My question is should a singleton really persist references across threads Maybe that is where the problem lies

    I can't find any documentation to answer that particular question. I did find this interesting text though in regards to remoting, which we have to contend with in our application from MSDN..it's almost like they're saying all rules are off when remoting:

    Singleton types never have more than one instance at any one time. If an instance exists, all client requests are serviced by that instance. If an instance does not exist, the server creates an instance and all subsequent client requests will be serviced by that instance. Because Singleton types have an associated default lifetime, clients will not always receive a reference to the same instance of the remotable class, even if there is never more than one instance available at any one time.

    Not sure if I follow all of that, but I think they're basically saying Singleton isn't truely singleton when it comes to remoting as I'm (and probably others) are use to viewing Singleton.

    And also in threading best practices they say to avoid having multiple threads point to the same resource, so I guess the question is how to make your unit tests run on a single thread

    I hope you can find a better answer...I'll be watching for one.

  • System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW...