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

Access violation using a C++ function from a DLL

本文关键字:访问冲突 函数 C++ DLL 中的 使用      更新时间:2023-10-16

我有一个用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工作正常且没有崩溃,尽管是相同的代码。

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

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

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

运行时设置(多线程 DLL、多线程调试 DLL 等)必须与要编译的所有模块匹配。 如果它们不匹配,请选择一个运行时,然后使用该运行时重新生成所有模块。