Random ArrayIndexOutOfBounds Exception

Hi everyone,

My project is randomly crashing due to ArrayIndexOutOfBounds with the following stack trace:

Code: Select all

java.lang.ArrayIndexOutOfBoundsException: length=18; index=18
    at org.jbox2d.dynamics.Island.add(Island.java:413)
    at org.jbox2d.dynamics.World.solve(World.java:927)
    at org.jbox2d.dynamics.World.step(World.java:565)

I have found a couple of other threads from users with this same problem, but no solution.

In my project, the user clicks to create a body at the mouse coordinates. The exception seems to be thrown almost immediately after a new body has been added in this way. I say this because the sprite for the new body is displayed and seems to move just a little before the exception is thrown.

I am not creating an bodies or destroying any bodies within the physics step (no body creation/destruction in my ContactListener implementation). Further, I am only creating bodies using the World.createBody(...) method, I never call "new Body()".

Any help would be greatly appreciated, as I cannot see a pattern to when this exception occurs.

Thanks,

Tom.
To clarify do you mean that the body is created without this exception sometimes however other times this exception is thrown, or that this exception is always thrown?
Hi

I fixed such error by adding "synchronized" to the method or around the code block that uses Box2D.
Box2D is not thread safe, and specifically on Android you tend to access it both from the UI thread (upon user events) and from the GL thread (upon draw). So you have to add the "synchronized" modifier around every block of Box2D code.

Michael
Wow how did I miss these replies?! Thanks guys.

WCJ, the exception is hardly ever thrown - bodies are created just fine. It is just occasionally that the exception gets thrown.

Michaelboccara, you're incredible - thanks for posting! I haven't tried it out yet, but that sounds like the solution I need. The randomness of the exception fits the bill for a threading error.

I'll let you know.

Tom.
I looked at the threads from which I am accessing Box2D and unless I'm missing something, I'm only calling Box2D code from one thread (user input events are obtained from the UI thread to the worker thread, and dealt with when the thread gets around to it).

I did find another bug though, one of my scripts was calling for destruction of an associated body when it reached a termination condition. However the script was not terminated properly, meaning that the destroyBody call was being made over and over using the same (already destroyed) body reference.

Does this sound like something that could lead to the ArrayIndexOutOfBoundsException I've been seeing? I haven't seen the exception since fixing that script bug, but there have been other times I thought I'd solved the problem and then it would come back to haunt me!

I did try to do a test where I intentionally called destroyBody multiple times on the same body reference, but this didn't cause any exception or error.


Tom.
Hi Tom,

Where are you calling World.step() from ? Do you call it from the draw() method of your GLRenderer ? If so, then I would guess that your user events are handled in the UI thread while the physics step happens in the GL thread, unless you use some message queue for user events between the UI thread and the GL thread.

Did you try adding "synchronized" around each of your Box2D calls' blocks ?

Regarding destroyBody, I believe that if your problem was an access to a destroyed object then your exception would be thrown in-stack with the destroyBody call...

Michael
Hi Michael,

All of my game logic and physics, including world.step() are done in one worker thread. I post only vertices and texture IDs to my render thread for drawing. The render thread doesn't hold any references to Box2D.

The input events on the UI thread are posted to the worker thread in a thread-safe way.

I did wrap all Box2D code with synchronization, but it didn't fix the problem.

The only thing that I've changed that has remedied this problem seems to be fixing that script mentioned in the previous post. I haven't seen the exception since 😐

Note: The destroyed body wasn't ever being accessed, it was just having destroyBody(body) called thousands of times. Whether that caused a problem...?

So stumped, just hoping that it doesn't resurface.



Tom.
Hi Tom

OK now I understand. So you are using some sort of message queue between the UI thread and the physics worker thread, right ?
I was just too much used to my way of using Box2D where I call World.step from within the draw callback (which is maybe not such a good idea).

Well, destroying a body several times is definitely not healthy, although I don't know what kinds of firewalls there are in the Box2D internals. So you may have found the culprit 🙂

Cheers
Hi Michael,

I think that might have been it - or some other weird effect of not killing that script I mentioned properly.

Thanks for you help, I was SO frustrated with that problem and your suggestion gave me some endurance to keep trying!

Tom.

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