JBox2D crashes when destroying all bodies

I have come across a strange problem when destroying bodies in JBox2D. I have no idea if this is a problem in the C++ version. When destroying a single body, there is no problem. But when I destroy all the bodies, I get a NegativeArraySizeException thrown in the Island.java constructor where bodyCapacity has dropped below zero. I noticed that, the first time I do this, everything is fine (my algorithm does not delete the m_groundBody created in the World.java constructor), but the second time, the world.getBodyCount() returns 0 rather than 1. The third time, the body count drops to -2, then -5. Here is the code I am using to destroy bodies.

Code:
private void selectAll()
   {
      for( Body body = world.getBodyList(); body != null; body = body.getNext() )
      {
         if( !body.isSelected() )
         {
            body.select();
            bodies.add( body );
         }
      }
   }


Code:
private void deleteSelected()
   {
      for( int i = 0; i < bodies.size(); i++ )
         if( !bodies.get(i).isGround() )
            world.destroyBody( bodies.get(i) );
      System.out.println( "bodies: " + world.getBodyCount() );
   }


I kind of used a temporary solution of including this after the m_bodyCount decrement in world.destroyBody()

Code:
if( m_bodyCount < 1 )
    m_bodyCount = 1;


I do this because my code always leaves the ground body intact.
I'm not sure what's going on there. Your code looks right, but something must be going wrong - if the body count goes down, something must be getting deleted, even though your code doesn't look like it should be deleting anything.

Are you making sure to clear the bodies array between uses?
Ah, you caught me again.

I probably just need to stop programming at night :P

In my deleteSelected() function, I don't remove the destroyed bodies from the array list. I guess I just forgot to add the line since I had just written a similar function where I didn't forget to remove the array list elements.

Sorry for posting prematurely.

Original thread: https://box2d.org/forum/viewtopic.php?t=3207