C++ 调用DLL函数时内存读取错误
我有两个项目:app.exe和engine.dll(在Visual Studio 2017中隐式链接lib)。当我试图从engine.dll调用app.exe中的此基本代码时,我得到内存读取错误(访问冲突)。下面是一个简短的例子:C++ 调用DLL函数时内存读取错误,c++,dll,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* C
// 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在引用中包含引擎。在调试器中单步执行,并查看堆栈/监视窗口中的所有指针。它显示正确的调用堆栈跟踪,但到达Engine.dll代码时,值已损坏