Box2D 和 dll "Access violation reading location"



是的,所以我在尝试编译项目时遇到了一个小问题。

我有一个项目制作了一个 dll,另一个项目使用它来制作 exe,两个项目都链接到 Box2D.lib(这是导致错误的原因吗?,如果是这样,当 dll 和 exe 同时使用时链接静态库的正确方法是什么)

b2World 的构造由 DLL 代码完成,但以下类由 exe 部分完成。

b2BodyDef def;
def.position = b2Vec2(10, 10);
def.type = b2_dynamicBody;
b2Body* body;
body = world->CreateBody(&def);
b2Vec2 sizeboxsim(10,10);
b2PolygonShape shape;
b2FixtureDef fixture;
fixture.shape = &shape;//this where the program returns the error
body->CreateFixture(&fixture);

错误如下:

在 GamePlayground .exe 中0x010B80C1处出现未经处理的异常:0xC0000005:访问违规读取位置0x41200000。

堆栈如下:

GamePlayground.exe!b2BlockAllocator::Allocate(int)
GamePlayground.exe!b2PolygonShape::Clone(class b2BlockAllocator *)
GamePlayground.exe!b2Fixture::Create(class b2BlockAllocator *,class b2Body *,struct b2FixtureDef const *)
GamePlayground.exe!b2Body::CreateFixture(struct b2FixtureDef const *)
GamePlayground.exe!testClass::testClass(b2World * world)
GamePlayground.exe!PlaygroundGame::PlaygroundGame()
GamePlayground.exe!main()
[External Code] 
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]  

有趣的是,如果您同时删除def.position = b2Vec2(10, 10);并且def.type = b2_dynamicBody;代码不会崩溃。

还需要注意的是,如果在 dll 中完成了相同的代码,即使从 exe 代码调用,它也能正常工作。

整个代码位于 https://github.com/Miniwoffer/Phoenix,尽管你需要boost,sfml和box2d来编译。

谢谢是提前

似乎默认情况下不会创建对象。尝试

b2PolygonShape shape;
b2FixtureDef fixture();

在开始时

b2BodyDef def();

最新更新