简体   繁体   中英

Box2D and dll “Access violation reading location”

Yeah so i have a little problem, when trying to compile my projects.

I have one project that makes a dll that the other project uses it to make a exe, both projects links to the Box2D.lib (is this whats causing the error?, if so whats the correct way to link a static library when used by both the dll and the exe)

The costruction of the b2World is done by the DLL code, but the following class is done by the exe part.

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);

The error is as follows:

Unhandled exception at 0x010B80C1 in GamePlayground.exe: 0xC0000005: Access violation reading location 0x41200000.

The stack is as follows:

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]  

The funny thig is if you remove both def.position = b2Vec2(10, 10); and def.type = b2_dynamicBody; the code does not crash.

Also somthing to note if the same code is done in the dll the it workes fine even when called from the exe code.

The whole code is located at https://github.com/Miniwoffer/Phoenix , altough you need boost,sfml and box2d to compile.

Thanks is advance

Seems that objects aren't created by default. Try

b2PolygonShape shape;
b2FixtureDef fixture();

And in beginning

b2BodyDef def();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM