调用DLL函数时内存读取错误

时间:2017-04-13 18:27:27

标签: c++ dll

我有两个项目:app.exe和engine.dll(在Visual Studio 2017中隐式地链接了lib)。当我尝试从engine.dll调用app.exe中的这个基本代码时,我得到内存读取错误(访问冲突)。这是一个简短的例子:

// In DLL .h
class Engine // pure virtual class
{
public:
    Engine() { }

/* pure virtual functions here ...*/

    static Engine* Create(DefaultAllocator& Allocator);
};

// In DLL CPP
Engine* Engine::Create(DefaultAllocator& Allocator)
{
    EngineImpl* impl = NEW_OBJ(Allocator, EngineImpl)(Allocator); // NEW_OBJ is a simple place holder
    // changing to 'new' didn't help too

    return impl;
}

void Engine::InitSomething()
{
// ERROR: Allocating anything here will be corrupted 
}

// In another DLL CPP
struct NewPlaceholder {};
#define NEW_OBJ(allocator, ...) new (NewPlaceholder(), (allocator).allocate(sizeof(__VA_ARGS__))) __VA_ARGS__

// Main.cpp from EXE
int main(int32 Argc, char* Argv[])
{
    static DefaultAllocator Allocator; // Has Allocate/Deallocate methods (malloc/free)

    Engine* engine = Engine::Create(Allocator); 
// And now as I call any function from Engine I get corrupted results
    engine->InitSomething();
}

两个项目都是/ MDd具有完全相同的设置。 App.exe在引用中有引擎。

0 个答案:

没有答案