Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer

Are you sure you are not calling DestroyJoint(weldJoint) more than once?

You have a for next and as far as I can tell, weldJoint is never set to a different value during this loop. Should the if condition matches more than once in this loop you will certainly have a problem if memory is being deleted each call to DestroyJoint().

Either add a break to exit the loop once DestroyJoint is called or set the value of weldJoint during the loop.

Also, make sure the DestroyJoint() function won't mess up the for next loop either if you don't intend to exit the loop on first match.

for(b2Body *b = world->GetBodyList(); b; b=b->GetNext())    {

If you call DestroyJoint() will it effectively delete what b is pointing at? If the answer is yes, then b = b->GetNext() is unsafe and unpredictable.

share|improve this answer
    
Thanks. DestroyJoint was being called several times since I didn't have a check for it. I tried the break, but it was giving the same error. I rather put a check and made the weldjoint null. Here's what I did: if (weldJoint == NULL) return; world->DestroyJoint(weldJoint); weldJoint = NULL; The issue with this is I am not able to create the weldjoint again. When the joint is destroyed I set a linear velocity to move the body towards another sprite to create another weldJoint. How can I solve this issue so I can create, destroy and recreate the weld joint unlimited times? –  colilo Aug 29 '12 at 17:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.