IndexOutOfBoundsException on Entity OnUpdate

  ... in the unlikely case you discovered a bug, post it here.

IndexOutOfBoundsException on Entity OnUpdate

Postby natan950 » Thu Oct 16, 2014 6:05 pm

Hi,

First of all, sine I am new to AndEngine, I must say that this bug may be caused by wrong usage.

I will start with the exception I see:
java.lang.IndexOutOfBoundsException
10-16 18:32:26.227: W/System.err(8924): at java.util.ArrayList.get(ArrayList.java:308)
10-16 18:32:26.227: W/System.err(8924): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
10-16 18:32:26.227: W/System.err(8924): at java.util.ArrayList.get(ArrayList.java:308)
10-16 18:32:26.227: W/System.err(8924): at org.andengine.entity.Entity.onManagedUpdate(Entity.java:1658)

I think this is enough to understand the problem.

In my game the bug appears when the bullet (extends from AnimatedSprite) hits the enemy (also extends from AnimatedSprite)(it happens on bullets OnUpdate function)
I unregister Update Handler of the bullet : Bullet.this.unregisterUpdateHandler(this);
(the registration made in the constructor registerUpdateHandler(new IUpdateHandler() {... )
I dispose the bullet : Bullet.this.dispose();
and detach it : Bullet.this.detachSelf();

In my opinion the bug happens in the "for loop" in onManagedUpdate function in Entity class line 1656.
the for loop runs until i < entityCount
the entity count has been set before the for loop "entityCount = children.size();" line 1655
since during the for I unregistered the Bullet from the updates, the new entityCount is smaller than the one was set.
which causes the IndexOutOfBoundsException.

My fix was to get the children.size() each iteration, but as you understand it is a bad idea for many reasons.

Am I doing something wrong? or is it a bug?
natan950
 
Posts: 1
Joined: Thu Oct 16, 2014 5:30 pm

Return to Bugs

Who is online

Users browsing this forum: No registered users and 4 guests