Search code examples
androidbox2dandenginegame-physicsphysics-engine

android andengine remove physics body crash application


I am writing a game in which i have to remove a rectangular brick with body on collision. The error occurs on *_physicsWorld.destroyBody(b);*

Here is my code snippet and logcat output (logcat says there is error in libc):

Code:

public void destroyBrick(IShape s, Body b, int i){

    PhysicsConnector facePhysicsConnector = _physicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(s);

        _physicsWorld.unregisterPhysicsConnector(facePhysicsConnector);

    _scene.unregisterTouchArea(s);

    _scene.detachChild(s);

    _physicsWorld.destroyBody(b);

    System.gc();

}

Logcat:

***08-19 12:58:17.491: A/libc(29832): /home/denbi/src/android/AndEngineExtensions/AndEnginePhysicsBox2DExtensionNew/jni/Box2D/Dynamics/b2World.cpp:134: void b2World::DestroyBody(b2Body*): assertion "IsLocked() == false" failed
08-19 12:58:17.501: A/libc(29832): Fatal signal 11 (SIGSEGV) at 0xdeadbaad (code=1), thread 29852 (UpdateThread)***

please help me out asap. thanks


Solution

  • You can't remove bodies during the execution of b2World::Step. You have to store the list of bodies you want to remove and actually remove them when Step exits.

    Example (C++):

    //in contact handling:
    m_bodiesToRemove.push_back(my_body);
    
    //after Step:
    for (b2Body *b : m_bodiesToRemove):
        m_world->DestroyBody(b);