• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Removing two objects at once failure

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1Patriarch K  Members

Posted 12 January 2013 - 07:41 PM

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, 12 January 2013 - 08:02 PM.

### #2RulerOfNothing  Members

Posted 12 January 2013 - 08:29 PM

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)

### #3Patriarch K  Members

Posted 13 January 2013 - 05:09 AM

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, 13 January 2013 - 05:42 AM.

### #4rip-off  Moderators

Posted 13 January 2013 - 08:08 AM

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:


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


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


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

### #5Patriarch K  Members

Posted 13 January 2013 - 10:30 AM

Thanks a lot! That worked perfectly fine for me. Problem solved

Edited by Patriarch K, 13 January 2013 - 10:30 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.