代码之家  ›  专栏  ›  技术社区  ›  Colin Desmond

为什么在单元测试时会看到moduleloadexceptionhandlerException异常

  •  4
  • Colin Desmond  · 技术社区  · 17 年前

    我们有一个混合模式程序集,包含VC++(使用MFC)和C++/CLI类。它是一个mfc扩展dll,在运行时加载到我们的mfc可执行文件中,一切正常。

    当我们从另一个C++/CLI程序集对单元中的非托管类进行单元测试时,每当我们试图创建一个非托管类的实例(通过new)时,会看到以下异常:

    Exception
    System.TypeInitializationException: The type initializer for '<Module>' threw an exception. ---> <CrtImplementationDetails>.ModuleLoadExceptionHandlerException: A nested exception occurred after the primary exception that caused the C++ module to fail to load.
     ---> System.Runtime.Serialization.SerializationException: Serialization error.
       at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
       at <CrtImplementationDetails>.DoCallBackInDefaultDomain(IntPtr function, Void* cookie)
       at <CrtImplementationDetails>.DoCallBackInDefaultDomain(IntPtr , Void* )
       at <CrtImplementationDetails>.LanguageSupport.InitializeDefaultAppDomain(LanguageSupport* ) in f:\dd\vctools\crt_bld\self_x86\crt\src\mstartup.cpp:line 518
       at <CrtImplementationDetails>.LanguageSupport._Initialize(LanguageSupport* ) in f:\dd\vctools\crt_bld\self_x86\crt\src\mstartup.cpp:line 721
       at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* ) in f:\dd\vctools\crt_bld\self_x86\crt\src\mstartup.cpp:line 875
       --- End of inner exception stack trace ---
       at <CrtImplementationDetails>.ThrowNestedModuleLoadException(Exception innerException, Exception nestedException)
       at <CrtImplementationDetails>.ThrowNestedModuleLoadException(Exception , Exception )
       at <CrtImplementationDetails>.LanguageSupport.Cleanup(LanguageSupport* , Exception innerException) in f:\dd\vctools\crt_bld\self_x86\crt\src\mstartup.cpp:line 841
       at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* ) in f:\dd\vctools\crt_bld\self_x86\crt\src\mstartup.cpp:line 883
       at .cctor() in f:\dd\vctools\crt_bld\self_x86\crt\src\mstartup.cpp:line 922
       --- End of inner exception stack trace ---
       at MSMContactDataTests.LoadUnknownItemTest()
    

    这看起来像是测试运行器加载程序集失败(在本例中为gallio.echo)。

    我还创建了一个小的C++控制台应用程序,并尝试了同样的东西。我可以正确地创建包含在程序集中的ref类的实例,但是当我尝试创建一个未排序的类时,我得到了相同的异常。

    有什么想法吗?

    编辑

    我本来要发布的控制台应用程序代码,打破了这里,但我已经重新编译了它,现在它的工作!这里是:

    #include "stdafx.h"
    
    using namespace System;
    
    #include "SCacheAssignment.h"
    
    int main(array<System::String ^> ^args)
    {
        Console::WriteLine(L"Hello World");
        SCacheAssignment* assignment = new SCacheAssignment(NULL, false);
        assignment->id = 2;
        Console::WriteLine(L"Hello World 2 " + assignment->id);
        return 0;
    }
    

    当我使用他的代码进行单元测试时:

    #include "stdafx.h"
    
    #include "PBSMSDataStoreUnitTests2.h"
    #include "SCacheAssignment.h"
    
    using namespace PBSMSDataStoreUnitTests2;
    
    void Class1::SomeTest()
    {
        Console::WriteLine(L"Hello World");
        SCacheAssignment* assignment = new SCacheAssignment(NULL, false);
        assignment->id = 2;
        Console::WriteLine(L"Hello World 2 " + assignment->id);
    }
    

    它坏了。这是测试程序集中的唯一测试。

    控制台应用程序是CLR控制台应用程序,其中的测试程序集位于CLR类库中。据我所知,它们使用相同的编译器选项。为什么一个管用一个不管用?

    1 回复  |  直到 17 年前
        1
  •  1
  •   Kim Gräsman    17 年前

    您的单元测试程序集也是c++/CLI吗?

    (根据新信息编辑)

    引人入胜的。我想知道这是否是由于混合模式dll中的托管静态构造函数失败造成的?或者在构造全局本机变量时出错?

    我发现在这个连接问题中提到了同样的问题: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=316549

    但恐怕没有解决办法或正确的诊断。

    推荐文章