使用DLL中的C ++函数访问冲突

时间:2015-01-29 20:33:28

标签: c++ opengl c++11 dll

我有一个用C ++编写的游戏引擎,我正在移植到Windows(来自Mac)。它使用C ++ 11和OpenGL,并且出于所有意图和目的,它运行!

我正在为我的游戏引擎使用DLL,它在运行时隐式链接到游戏.exe。问题是,当我尝试使用DLL中的实用程序类FileSystem来查找资源文件(纹理,但我认为它不重要)时,我收到此错误:

First-chance exception at 0x00007FF9CF988830 (PocoFoundation64.dll) in TestEquinox.exe: 0xC0000005: Access violation reading location 0x000000136A4FF000.

当我从DLL调用我的FileSystem类的这个方法时,问题出现了(它设计为采用文件名/部分路径,并且它在不同的地方查找以找到完整路径):

Poco::Path FileSystem::Get(const std::string &filename) {
std::vector<Poco::Path> paths = {
        filename,
        ResourceFolder() / filename //<<<<< ERROR HERE
    };

    for (const Poco::Path &path : paths) {
        try {
            if (Poco::File(path).exists()) {
                return path;
            }
        } catch (...) { }
    }

    Logger("FileSystem", std::cerr) << "Could not find file '" << filename << "'!";
    return {};
}

Visual Studio将错误显示为调用ResourceFolder(),来自同一类的另一个方法,也在DLL中。这似乎是这样的:

Poco::Path FileSystem::ResourceFolder() {
    Poco::Path userData;

    //--SNIP-- (other OS's #ifdef'd here)
    // GAME->StartupPath is a std::string containing the exe's parent folder
    userData = (Poco::Path(GAME->StartupPath).parent() / "Resources").makeDirectory();
    //--SNIP-- (and here)

    try {
        if (!Poco::File(userData).exists()) {
            Poco::File(userData).createDirectories();
        }
    } catch (...) {}

    return userData;
}

从它的外观来看,它与Poco的数据类型没有被正确实例化有关吗?我使用所有相同的编译器设置(64位,多字节字符集,VS2013)从源代码构建它,所以我不知道它是如何成为名称修改/数据布局问题。

还有一点要注意 - 我将整个FileSystem类从DLL复制到我的游戏项目的本地类,称为FileSystem2。使用相同的参数调用FileSystem2::Get可以正常工作而不会崩溃,尽管代码相同。

希望有人能指出我正确的方向?!

1 个答案:

答案 0 :(得分:1)

通常这样的错误源于使用模块使用的不兼容的运行时库。请检查以下Visual Studio属性中的所有模块:

Project Properties -> C/C++ -> Code Generation -> Runtime Library

运行时设置(多线程DLL,多线程调试DLL等)必须与您正在编译的所有模块匹配。如果它们不匹配,请选择一个运行时,并使用该运行时重建所有模块。