[LibGDX - Box2D] Deleting bodies

Hi,
So I want to delete some bodies after they collide.
I have a Contact class which is a ContactListener class. In the postsolve method,
screen is my game screen, and delete is an array of bodies which I’m trying to remove:

if(contact.getFixtureA().getBody().getUserData().equals("player")&&contact.getFixtureB().getBody().getUserData().equals("gold")){
	screen.delete.add(contact.getFixtureB().getBody()); 
}

Then I’m deleting my bodies like this:

Iterator<Body> i = delete.iterator();
if(!world.isLocked()){
	while(i.hasNext()){
		Body b = i.next();
		world.destroyBody(b);
	}
}

But it gives me an error: Assertion failed: (m_bodyCount < m_bodyCapacity), function Add, file ./Box2D/Dynamics/b2Island.h, line 54.
I run this code just after I set the world to step.
Step is like this: world.step(1/60f, 6, 4);
Played with the numbers, didn’t help.
I also have a randomGenerate() method which generates a platform, and adds it to the world. I thought that could be the reason because the error includes the word “Add” :smiley: But the problem persisted after I removed that method, meaning I only had two bodies in the world.

I also tried setting world step to 0, adding b.setAwake(false) and b.setActive(false) but nothing changed. The problem is always the same:

[quote]Assertion failed: (m_bodyCount < m_bodyCapacity), function Add, file ./Box2D/Dynamics/b2Island.h, line 54.
[/quote]
Now, I have no C/C++ experience but here is the 54th line:

void Add(b2Body* body) <-- 54th line. 
{
	b2Assert(m_bodyCount < m_bodyCapacity);
	body->m_islandIndex = m_bodyCount;
	m_bodies[m_bodyCount] = body;
	++m_bodyCount;
}

Please note that this happens even when there are only two bodies in the screen and nothing else is going on.

What can I do?

It was so simple. It was so simple that nobody ever had the same issue on the internet.
It was so simple that I feel stupid because I didn’t think it before.

So, I have an array there, right? That’s called delete. When the collision occurs, I have one element in the array. When the world is not locked, that is, the world is not stepping, I remove the body from the world. But not from the damn array.

This code always looked a little bit simple to me:

Iterator<Body> i = delete.iterator();
if(!world.isLocked()){
   while(i.hasNext()){
      Body b = i.next();
      world.destroyBody(b);
   }
}

Add this, and it is done:

i.remove();

Code is not important, just delete the object as well.

I’m not sure if I ever come across to a situation where this is problematic, but at least I should have thought that I needed to remove the object from the array as well. It would try to destroy a null ‘thing’ from the world again once the world is not locked.

Damn…

Damn 5 hours and I did nothing. I could be having a drink, I could be reading something useful, I could be meditating but no… That problem had to be stuck in my head. And that small damn idea had a business in somewhere else, that it didn’t bother to check if it is needed in my head.

Man I’m furious right now.