1

i'm building a cocos2dx game with box2d. Everything works fine except when I deploy the app on the android device, the game crashes when i restart a level couple of times. Since I cannot debug on eclipse, using CCLogs I've managed to tracked down the the exact line where the crash is happening. Here's the code..

void GameLayer::update(float dt)
{

    int velocityIterations = 8;
    int positionIterations = 1;
    CCLog("1 bodycount %d",world->GetBodyCount());
    world->Step(dt, velocityIterations, positionIterations);

    for(b2Body *b = world->GetBodyList(); b; b=b->GetNext()) { 
        //CCLog("Counter %d",counter);
        if (b->GetUserData() != NULL) {
            CCLog("1.5");
            CCSprite *sprite = (CCSprite *)b->GetUserData(); 
            CCLog("2");
            sprite!=NULL?CCLog("sprite not null"):CCLog("sprite is null");
            CCLog("2.1");
            CCLog("3 sprite tag:%d",sprite->getTag());
            sprite->setPosition( ccp(b->GetPosition().x * PTM_RATIO,
                                    b->GetPosition().y * PTM_RATIO));
            sprite->setRotation( -1 * CC_RADIANS_TO_DEGREES(b->GetAngle()));


            //......more code.....


        }
    }
}

This is the update loop where the box2d bodies get updated. The line before CCLog("2.1") I get sprite not null. But after that when I try to print the sprite's tag(CCLog("3 sprite tag:%d",sprite->getTag());), it crashes. In the logcat I just get Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 30903 (Thread-2408)

Doesn't that mean the CCSprite is NULL? Any idea what could be going wrong? I can provide more detail/code if needed.

2
  • 2
    Just because the pointer value held by sprite is not NULL it does not mean it points to a valid object. Since you're doing a C-style cast (which omits dynamic_cast from the conversion) there's no guarantee that it even points to a CCSprite object. Jan 25, 2014 at 20:36
  • Okay. So how do I check if sprite actually points to a valid CCSprite? Jan 25, 2014 at 21:26

1 Answer 1

0

Since this sprite is in a physics body's userData, the userdata field won't be cleaned up when the sprite is removed from cocos2d node hierarchy, leaving a dangling pointer. Check any occurance of a sprite with a body that, when removed from the node hierarchy or when deallocating doesn't disconnect itself from its body by setting userdata to NULL respectively doesn't actually remove its physics body from the world as it probably should in this case.

3
  • I don't quite understand. So, you're suggesting its something to do with the way i remove my bodies and sprite? I do it like this. this->removeChild((CCSprite*)ball->GetUserData()); world->DestroyBody(ball); ball=NULL; Jan 25, 2014 at 21:29
  • try adding ball->SetUserData(NULL) before destroying the body - destroying it may or may not happen instantly
    – CodeSmile
    Jan 25, 2014 at 22:52
  • Nope. Didn't' work either. I came across another similar crash involving a CCSprite which isn't any b2Body's userdata. I keep adding and removing this sprite to the CCLayer over the course of the game. Jan 26, 2014 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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