Reputation: 1568
Im creating box2d polygons, i would like to create the fixture outside of the method that attaches the fixture to the body, but when i do this i get a access violation.
i have stepped through the code and box2d is definitely getting the fixture object.
when i attach the fixture to the body inside the method it works fine. when i try in the calling method it throws access violation error
b2FixtureDef* LineSegment::GenerateFixture(b2Vec2 vertices[4],b2Body* body,b2World* world){
b2BodyDef bodyDef;
//bodyDef.type = b2_kinematicBody;
bodyDef.position.Set(_currentStepStartPosition.x,_currentStepStartPosition.y);
body =world->CreateBody(&bodyDef);
int32 count = 4;
b2PolygonShape polygon;
polygon.Set(vertices, count);
b2FixtureDef fixtureDef2;
fixtureDef2.shape = &polygon;
fixtureDef2.density = 1.0f;
fixtureDef2.friction = 0.3f;
body->CreateFixture(&fixtureDef2);/// works if i attatch the fixture here here
return &fixtureDef2;
}
the calling method it fails when i attach the fixture here
bool LineSegment::GenerateNextBody(b2Body* retBody){
b2BodyDef bodyDef;
bodyDef.type = b2_staticBody;
bodyDef.position.Set(_currentStepStartPosition.x,_currentStepStartPosition.y);
retBody =world->CreateBody(&bodyDef);
_currentStep++;
b2Vec2 vertices[4];
if(_inclinePerStep != 0){
GetVertsInclineSquare(vertices,_stepWidth,_thickness,_inclinePerStep);
}else{
GetVertsSquare(vertices,_stepWidth,_thickness);
}
if(_currentStep == 1){
_GameWorldVerticies[0] =b2Vec2((_currentStepStartPosition.x+vertices[0].x)*PTM_RATIO,(_currentStepStartPosition.y+vertices[0].y)*PTM_RATIO);
_GameWorldVerticies[3] =b2Vec2((_currentStepStartPosition.x+vertices[3].x)*PTM_RATIO,(_currentStepStartPosition.y+vertices[3].y)*PTM_RATIO);
}
b2FixtureDef* fixture = GenerateFixture(vertices,retBody,world);
//retBody->CreateFixture(fixture); throws a access violation if i attatch the fixture here
_currentStepStartPosition.x += vertices[2].x+(vertices[2].x);
_currentStepStartPosition.y +=(_inclinePerStep/2);
if(_steps <= _currentStep){
_GameWorldVerticies[1] =b2Vec2((_currentStepStartPosition.x+vertices[1].x)*PTM_RATIO,(_currentStepStartPosition.y+vertices[1].y)*PTM_RATIO);
_GameWorldVerticies[2] =b2Vec2((_currentStepStartPosition.x+vertices[2].x)*PTM_RATIO,(_currentStepStartPosition.y+vertices[2].y)*PTM_RATIO);
return false;
}
return true;
}
UPDATE
new snippet
b2FixtureDef* fixtureDef2 = new b2FixtureDef();
fixtureDef2->shape = &polygon;
fixtureDef2->density = 1.0f;
fixtureDef2->friction = 0.3f;
//body->CreateFixture(&fixtureDef2);/// works if i attatch the fixture here here
return fixtureDef2;
Upvotes: 1
Views: 391
Reputation: 409166
The problem is that you return a pointer to a local variable in GenerateFixture
, which is undefined behavior. The variable fixtureDef2
is on the stack, and when the function returns that area of the stack is not valid any more. When you late call the CreateFixture
function, the pointer will now point into the area of the stack for that function.
To solve this you could create it on the heap with e.g. new
.
Upvotes: 2