c++ - 如果在添加 fmod Visual C++ 测试后在一次测试中出现特定代码行,则所有测试均失败,无法获取类型,并出现 FileNotFoundException

标签 c++ clr mstest fmod

我已经弄清楚是什么导致了这个问题,但我仍然不知道为什么 - 它发生在我开始使用 fmod 时,它一定与链接器决定如何带来有关输入并执行静态库和 .dll。我的测试代码是一个静态库;它指的是 fmodex_vc,另一个静态库,它在某个时候(虽然我不知道什么时候)决定加载它的 fmodex.dll。 (它与其他所有东西都在同一个目录中,所以我不知道为什么找不到它。)据我所知,被测代码绝对不会调用 fmod 初始化函数,但也许 fmod 有一些初始化自身并加载到 dll 中的静态全局初始化程序?并且只有在使用它的模块中的代码被...使用时才会引入该代码?

我正在使用 Visual Studio 测试框架测试非托管 C++ 代码,当我开始使用 fmod 时它停止工作:每个测试,甚至是什么都不做的“测试”测试,都会报告(包装以提高可读性):

Unable to get type SlidersTest.UnitTest1, SlidersTest.
    Error: System.IO.FileNotFoundException: 
    The specified module could not be found.
    (Exception from HRESULT: 0x8007007E)

经过反复试验,排除.cpp 文件并重新添加它们,我发现只有一个测试文件引发了问题;并且仅当此行被调用时才会执行:

EntityMgr::Init();

有趣的是,如果该行在代码中,所有测试都会开始失败并显示该消息。 EntityMgr::Init() 是一个功能很少的函数:

EntityMgr* EntityMgr::instG = null;

void EntityMgr::Init()
{
   instG = new EntityMgr;
}

class EntityMgr
{
private:
   static EntityMgr* instG;
public:
   EntityMgr()   // does nothing beyond the default 
   {
   }

   static void Init();
   static EntityMgr* Inst() { return instG; }

   ...

   vector<Entity> entitiesG;
};

Entity,FWIW,是一个非常普通的类,没有指针,只有用于其字段的各种 float

  • 无论我如何运行测试(从测试 View 、选择运行、全部运行、从命令行运行、从测试菜单),我都会收到错误。
  • 尝试使用调试器进入测试失败 - 在调试器进入之前测试失败。将调试器设置为在系统异常时中断也没有任何效果。
  • 被测代码是静态 .lib。 CLR 支持是 /clr
  • 哦,这就是:如果我调用一个静态 Entity 成员函数,同样的处理。如果我将所说的静态函数移到类之外,同样的处理。但是,如果我将该功能移动到另一个模块,那就没问题了。

如果我将调试器设置为在出现任何异常时中断,我确实会得到一些有趣的东西:

First-chance exception at 0x7c812aeb in vstesthost.exe: Microsoft C++ exception: HRException at memory location 0x05129890..

当然,该位置没有源代码。这是调用堆栈:

     kernel32.dll!7c812aeb()    
    [Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]  
    kernel32.dll!7c812aeb()     
    [External Code] 
    msvcr80.dll!78158ed7()  
    msvcr80.dll!78158e34()  
    msvcr80.dll!78158047()  
    msvcr80.dll!7815850e()  
    msvcr80.dll!78158872()  
    msvcr80.dll!78158a57()  
    msvcr80.dll!78158b11()  
    ntdll.dll!7c9032a8()    
    ntdll.dll!7c90327a()    
    ntdll.dll!7c92a9ef()    
    ntdll.dll!7c90e46a()    
    kernel32.dll!7c812aeb()     
    kernel32.dll!7c812aeb()     
    kernel32.dll!7c812aeb()     
    msvcr80.dll!78139c4d()  
    msvcr80.dll!781323ff()  
    msctf.dll!74755764()    
    msctf.dll!74721557()    
    ws2_32.dll!71ab12bb()   
    ntdll.dll!7c90118a()    
    ntdll.dll!7c91b084()    
    ntdll.dll!7c90de7c()    
    ntdll.dll!7c90d04c()    
    ntdll.dll!7c90e43f()    
    kernel32.dll!7c80b713()     

这是 mstest 报告的堆栈跟踪 - 我没有从中得到任何有用的信息。

Unable to get type SlidersTest.game_EntityMgr_test, SlidersTest. Error: System.IO.FileNotFoundException: The specified module could not be found. (Exception from HRESULT: 0x8007007E)
   at System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
   at System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
   at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
   at System.Reflection.Assembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, StackCrawlMark& stackMark)
   at System.Reflection.Assembly.LoadFrom(String assemblyFile)
   at Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestExecuter.GetType(UnitTestElement unitTest, String type)
   at Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestExecuter.ResolveMethods().

为什么 fmod 这样做?

最佳答案

我建议在 Dependency Walker 的配置文件模式下运行您的应用程序 (http://www.dependencywalker.com/)。它可以记录所有加载 DLL 和 EXE 的尝试,以及产生的错误代码 - 听起来很可能文件未找到错误来自间接依赖 - 可能是从链接 FMod 中提取的。

如果是这种情况,使用 Dependency Walker 分析您的应用程序将显示一次或多次加载库的失败尝试。其中之一将是对错误负责的人。

关于c++ - 如果在添加 fmod Visual C++ 测试后在一次测试中出现特定代码行,则所有测试均失败,无法获取类型,并出现 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/735299/

相关文章:

c++ - opencv 将较小的图片放入较大的拷贝中无法正常工作

c++ - 在 Linux 中使用带有 C++ 的 boost-python

c++ - 错误 : switch quantity not an integer

c# - 如何衡量 .NET 中托管线程的性能

c# - 哪个是创建捕获变量/闭包的代码?

c++ - VS2012 MSTest 无法捕获断言, native c++

c++ - a *= b + c 展开成什么?

c# - Garbage Collection Modes : If 2 apps exist on a server, "Server Mode"难道是劫以还债?

c# - 如何将同步上下文添加到异步测试方法

c# - Visual Studio Ordered 测试运行两次