Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

on Android application using libgdx, when I use World.destroyBody( Body body ) method, once in a while the application suddenly shuts down.

Is there some setting I need to do with body collision or Box2DDebugRenderer before I destroy bodies?

Below is the source I use for destroying bodies.

private void deleteUnusedObject( ) {
    for( Iterator<Body> iter = mWorld.getBodies() ; iter.hasNext() ; ){
        Body body = ) ;
        if( body.getUserData( ) != null ) {
            Box2DUserData data = (Box2DUserData) body.getUserData( ) ;
            if( ! data.getActFlag() ) {
                if( body != null ) {
                    mWorld.destroyBody( body ) ;


share|improve this question
Do you have any error messages? Why would you check if the body != null in the inner if statement when it is first used in your first if statement body.getUserData() != null. Second, what does mWorld.destroyBody( body ); do? Do you have any other threads running? – Luis Estrada Mar 2 '12 at 15:01
Do you do any contact filtering? When do you call deleteUnusedObjects? Are you sure youre not storing and using any references to destroyed bodies somewhere? – Majd Mar 4 '12 at 12:16

If you are trying to destroy the body in a CollisonEvent, this happens mid-step so you can't do that. Your best bet is to build a list of objects you would like to destroy and have them removed next frame.

share|improve this answer
This is most likely the problem. @minami I did post a solution here that you would need to call outside the world.step()… – Chuck D Aug 3 '12 at 20:57

I also face the same problem in libgdx. But I found an alternate solution about this is. The following is the way to work same as destroyBody


this will made body inactive in the physics world and in the dispose method we destroy the body.

share|improve this answer

Make sure you are not trying to destroy the body inside of world.step(). If you are destroying it from a ContactListener or something similar, you'll have to mark the body to be destroyed later and not during world.setp().

share|improve this answer

In your render method check if the world is locked, with world.islocked(), if it isn´t locked, and call immediatly the destroy body.

I suppose that you error is an assertion from Box2D

render(float arg0)
            destroy your bodies here

share|improve this answer

This is the best way to do it, i read it in the Box2D manual. You have to call this just after CURRENT_WORLD.step(..,...,...);

Iterator<Body> i = mWorld.getBodies();
while (i.hasNext()) {
    Body oBj=node;;
    Box2DUserData data = (Box2DUserData ) oBj.getUserData();
    if(data!=null  &&  !data.getActFlag()){
share|improve this answer

You need to destroy a body after world.step()

share|improve this answer

Your Answer


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.