# Removing two objects at once failure

This topic is 1835 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm making a java game at the moment and when I happen to kill two enemies at the exactly same time then the game crashes. I suppose it's something with threads and serializable or something. But I don't know. I use the following code when I'm removing objects from the arrayList. The problem appears when I kill two enemies at EXACTLY the same time. Then it freezes and say:

at java.util.ArrayList\$Itr.remove(Unknown Source)
at Main.checkCollisions(Main.java:168)
at Main.run(Main.java:400)

// Skott med fiende
Iterator<Bullet> b = bubblor.iterator();
while(b.hasNext()){
Iterator<Enemy> e = enemies.iterator();
Bullet bob = b.next();
while(e.hasNext()){

Enemy fof = e.next();
Rectangle r1 = fof.getBounds();
Rectangle r2 = bob.getBounds();
if(r1.intersects(r2) ){
b.remove();
e.remove();
} } } Edited by Patriarch K

##### Share on other sites
From the documentation of the ArrayList:
Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.)
This means that you'll have to prevent two threads from accessing the ArrayList at the same time; this could be accomplished by having a wrapper of the ArrayList that queues up addition and deletion operations, or by eschewing the use of multiple threads entirely (although you may have a good reason for going threaded)

##### Share on other sites
That must be the problem then. But I'm not very into that kind of stuff. Could you explain a little bit how to do what you mention and give some kind of code example?
Can I just do like this and NOTHING ELSE? This does not work because I get problems on the b.remove() every time when two bullets is shot at the same time and removed at the same time.

List list = Collections.synchronizedList(new ArrayList(...));

and:

synchronized(bubblor) {
Iterator<Test> b = bubblor.iterator();
while(b.hasNext()){
synchronized(enemies) {
Iterator<Enemy> e = enemies.iterator();
Test bob = b.next();
while(e.hasNext()){

Enemy fof = e.next();
Rectangle r1 = fof.getBounds();
Rectangle r2 = bob.getBounds();
if(r1.intersects(r2) ){
b.remove(); Edited by Patriarch K

##### Share on other sites

You have at least one bug that is not thread related. If you are using threads, you may have more bugs.

The problem is, imagine that a given bullet intersects two enemies in a single frame. What your code does is call remove() twice on the bullet iterator in this case.

The fix is to only remove the bullet iterator once, if any collisions have occurred:

  

Iterator<Bullet> b = bullets.iterator();
 while(b.hasNext()){     Bullet bullet = b.next();     Rectangle bulletBounds = bullet.getBounds();     boolean collision = false;     Iterator<Enemy> e = enemies.iterator();     while(e.hasNext()){         Enemy enemy = e.next();         Rectangle enemyBounds = enemy.getBounds();         if(enemyBounds.intersects(bulletBounds) ){             collision = true;             e.remove();         }      }           if(collision) {         b.remove();     } }  

Perhaps better is to create a helper function to clarify the logic:

  

private static int removeIntersectingEnemies(Rectangle bounds, List<Enemy> enemies) {
     int enemiesRemoved = 0;     Iterator<Enemy> iterator = enemies.iterator();     while(iterator.hasNext()){         Enemy enemy = iterator.next();         Rectangle enemyBounds = enemy.getBounds();         if(enemyBounds.intersects(bounds) ){             ++enemiesRemoved;             iterator.remove();         }      }     return enemiesRemoved; }   Iterator<Bullet> iterator = bullets.iterator(); while(iterator.hasNext()){     Bullet bullet = iterator.next();     int enemiesRemoved = removeIntersectingEnemies(bullet.getBounds(), enemies);     if(enemiesRemoved > 0) {         iterator.remove();     } }

##### Share on other sites
Thanks a lot! That worked perfectly fine for me. Problem solved :) Edited by Patriarch K