Error in destroying object in Box2D/LibGDX

I'm trying to delete an object when a collision happens. I have put the following code in the render method of the object so it would be outside of the physics calculations.

public void render(SpriteBatch spriteBatch) {
// some other code...
body.setActive(false);
body.getWorld().destroyBody(body);


But I'm getting an run-time error which crashes the JVM and shows,

AL lib: alc_cleanup: 1 device not closed

Assertion failed!

Program: C:\Program Files\Java\jre6\bin\javaw.exe File: /var/lib/hudson/jobs/libgdx-git/workspace/gdx/jni/Box2D/Dynamics/b2World.cpp, Line 133

Expression: m_bodyCount > 0

Can anyone help me here?

• it seems you are calling destroyBody() with a body count < 1. Are you sure it is being created succesfully? – petervaz Jun 21 '13 at 14:41
• When this occures I think the only 3 possiblities are that you are destroying the body already before you call "body.getWorld().destroyBody(body);", you are destroying it while the step is occuring, or you are destroying it during a listener. Need more code to know for sure though. – ClassicThunder Jun 21 '13 at 14:44

I was having the same problem. I had some edges that were being created and destroyed on the fly. It started crashing on me, and that lead me to this post. petervaz seemed to have the right idea. When I was destroying a body, I was not checking to see if it "existed" or not. For me, it was a simple check of a boolean value I had already set up.

if (on)
{
on = false;
world.getPhysicsWorld().destroyBody(body);
}


There have not been any of those crashes since I put that check in.

You must destroy your body after collision happened, for that override your EndContact() method.

• Maybe explaining why MUST would be nice. – sm4 Jun 21 '13 at 15:06
• What sm4 said - regardless of whether this solves the problem, it's of no use to anyone else without reasoning (and helping others in the future is sort of the point of SE sites). – Polar Jun 21 '13 at 15:56
• Your answer should not be too straight forward! It has to be more guidelines or explainig WHY you need it and how. Worst answer format ever. – David Dimalanta Jul 12 '13 at 6:19