AppDomains and Static Local Variables (C++/CLI)

"Leon Raj" <[email protected]> Mon, 11 Aug 2008 16:44:42 +1000
Newsgroups gmane.comp.windows.dotnet.nunit.user
Message-ID <[email protected]>
I am trying to use Nunit to test a managed interface that is implemented

in C++/CLI, and I'm coming across a problem where because Nunit loads
the 
assembly under test in a different AppDomain from the "current", 
the assertion described in the link below is triggered.

"Creating static local variable from non-default appdomain in /clr build

causes assertion"
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?Fee
dbackID=171930

Is there a way to use Nunit and have our assembly to test loaded in the 
same AppDomain ?

An example of the code that reproduces this is attached below.

Regards,
Leon


////////////////////////////////////////////////////////////////////////
//
// ManagedCppLibrary.h

namespace ManagedCppLibrary {

    public ref class FooBar
    {
    public:
        bool DoSomething();
    };
}

////////////////////////////////////////////////////////////////////////
//
// ManagedCppLibrary.cpp

class Native
{
public:
    Native()
    {
        m_Member = "Hello";
    }

    static Native& Instance()
    {
        // Must have a non-trivial constructor, so that a destructor
        // is registered with the CRT
        static Native _Instance;
        return _Instance;
    }

    std::string m_Member;
};

bool FooBar::DoSomething()
{
    Native::Instance();
    return true;
}

////////////////////////////////////////////////////////////////////////
//
// TestFixture.cs

[TestFixture]
public class TestFixture
{
    public TestFixture()
    {
    }

    [Test]
    public void TestCase()
    {
        ManagedCppLibrary.FooBar fooBar = new FooBar();
        Assert.IsTrue(fooBar.DoSomething());

    }
}

////////////////////////////////////////////////////////////////////////
//
// Assertion occurs in here, on completion of the construction of the
// static local variable

extern "C" int __clrcall _atexit_m( _CPVFV func)
{
    MANAGED_ASSERT(AppDomain::CurrentDomain->IsDefaultAppDomain(), 
	"This function must be called in the default domain");

    int retval = _atexit_helper((_CPVFV)_encode_pointer(func), 
	&__exit_list_size, 
	&__onexitend_m, &__onexitbegin_m);

    return retval;

}



-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/