EXC_BAD_ACCESS error when box2d joint is destroyed
- by colilo
When I destroy the weldJoint in the update method (see below) I get an EXC_BAD_ACCESS error pointing to the line
world->DestroyJoint(weldJoint);
in the update method below:
-(void) update: (ccTime) dt
{
int32 velocityIterations = 8;
int32 positionIterations = 1;
// Instruct the world to perform a single step of simulation. It is
// generally best to keep the time step and iterations fixed.
world->Step(dt, velocityIterations, positionIterations);
// using the iterator pos over the set
std::set<BodyPair *>::iterator pos;
for(pos = bodiesForJoints.begin(); pos != bodiesForJoints.end(); ++pos)
{
b2WeldJointDef weldJointDef;
BodyPair *bodyPair = *pos;
b2Body *bodyA = bodyPair->bodyA;
b2Body *bodyB = bodyPair->bodyB;
weldJointDef.Initialize(bodyA, bodyB, bodyA->GetWorldCenter());
weldJointDef.collideConnected = false;
weldJoint = (b2WeldJoint*) world->CreateJoint(&weldJointDef);
// Free the structure we allocated earlier.
free(bodyPair);
// Remove the entry from the set.
bodiesForJoints.erase(pos);
}
for(b2Body *b = world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData() != NULL)
{
CCSprite *mainSprite = (CCSprite*)b->GetUserData();
if (mainSprite.tag == 1) {
mainSprite.position = CGPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);
CGPoint mainSpritePosition = mainSprite.position;
if (mainSprite.isMoved) {
world->DestroyJoint(weldJoint);
}
}
}
}
}
In the HelloWorldLayer.h I set the weldJoint with the assign property. Am I destroying the joint in the wrong way? I would really appreciate any help. Thanks