Strange ArrayIndexOutOfBoundsException :(

Hey guys, sometimes after I run my game for a while (2-3 mins) i get this strange exception message and i dont know what are the reasons why this happened...I tried to exclude some of my games objects, somehow i tried to detect witch object is the reason of this exception but I can not figure out...The whole code is fair big(350 kb+) so I dont think will help to post it here :slight_smile:



Jan 29, 2012 10:59:13 AM com.jme3.app.Application handleError

SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]

java.lang.ArrayIndexOutOfBoundsException

at java.lang.System.arraycopy(Native Method)

at java.util.ArrayList.toArray(Unknown Source)

at com.jme3.util.SafeArrayList.getArray(SafeArrayList.java:125)

at com.jme3.scene.Spatial.runControlRender(Spatial.java:572)

at com.jme3.renderer.RenderManager.renderScene(RenderManager.java:784)

at com.jme3.renderer.RenderManager.renderViewPort(RenderManager.java:1117)

at com.jme3.renderer.RenderManager.render(RenderManager.java:1168)

at com.jme3.app.SimpleApplication.update(SimpleApplication.java:266)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:149)

at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:185)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:223)

at java.lang.Thread.run(Unknown Source)



Please help me!

Thank you,

Pengu.

Are you working with multiple threads?

yes, but i use



[java]

MyGame.rootNode.addControl(new UpdateControl());

MyGame.rootNode.getControl(UpdateControl.class).enqueue(new Callable<Object>()

{

public Object call() throws Exception

{

// CODE HERE!!!

return null;

}

});[/java]

Hi,



Iā€™m experiencing the same issue when using LodControl :



SEVERE Application 21:38:28 Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at java.util.ArrayList.toArray(ArrayList.java:361)
at com.jme3.util.SafeArrayList.getArray(SafeArrayList.java:125)
at com.jme3.scene.Spatial.runControlRender(Spatial.java:559)
at com.jme3.renderer.RenderManager.renderScene(RenderManager.java:784)
at com.jme3.renderer.RenderManager.renderScene(RenderManager.java:794)
at com.jme3.renderer.RenderManager.renderScene(RenderManager.java:794)
at com.jme3.renderer.RenderManager.renderViewPort(RenderManager.java:1117)
at com.jme3.renderer.RenderManager.render(RenderManager.java:1168)
at com.example.Main.update(Main.java:414)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:149)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:182)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:223)
at java.lang.Thread.run(Thread.java:722)


I'm also using multiple threads, and I have a lot of Spatial with LodControls. However all my calls to jme are made using the enqueue(Callable) method as pengu above. What could be going wrong ?

This issue may be related to another one I'm having with LODs too, this time I have an NPE :

SEVERE Application 21:42:56 Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.NullPointerException
at com.jme3.scene.Spatial.runControlRender(Spatial.java:560)
at com.jme3.renderer.RenderManager.renderScene(RenderManager.java:784)
at com.jme3.renderer.RenderManager.renderScene(RenderManager.java:794)
at com.jme3.renderer.RenderManager.renderScene(RenderManager.java:794)
at com.jme3.renderer.RenderManager.renderViewPort(RenderManager.java:1117)
at com.jme3.renderer.RenderManager.render(RenderManager.java:1168)
at com.example.Main.update(Main.java:414)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:149)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:182)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:223)
at java.lang.Thread.run(Thread.java:722)


If I go to the code of Spatial line 559 to 561 I can read this :

[java]for (Control c : controls.getArray()) {
c.render(rm, vp);
}[/java]

In both cases this seems to be related to the SafeArrayList class. Could there be a thread safety issue here ?
@vivi said:
In both cases this seems to be related to the SafeArrayList class. Could there be a thread safety issue here ?


Yes. Because SafeArrayList is specifically NOT thread safe. Something in your application is modifying things from different threads. It's hard for us to guess what... but it's a near certainty.

Basically, it looks like between the time the control list size was retrieved and the control list was copied into an array, a control was added.

That's bad. That means that you are adding controls from a thread other than the render thread... and if you get this error frequently then that means you are doing it a lot. If you find yourself adding and removing controls a lot then it may be the sign of a poor design.
@pspeed said:
If you find yourself adding and removing controls a lot then it may be the sign of a poor design.


How does one handle this considering a multi-player game? No choice but to add/remove controls if new players enter/leave the game. However, I should also mention I never see this error... so it may be the ugly (but effective) way I am adding/removing these?
@t0neg0d said:
How does one handle this considering a multi-player game? No choice but to add/remove controls if new players enter/leave the game. However, I should also mention I never see this error... so it may be the ugly (but effective) way I am adding/removing these?


A) you are still rarely adding or removing anything. If you did it wrong you would still have a one in a million chance of missing.

B) you are more likely removing a spatial than a control in this case.

Even if someone has a threading bug, one like this would happen rarely. I'd guess it's a sign that controls are removed and added very often... like potentially per frame, even. If you see a rare timing bug frequently then you are triggering the situation a lot.
@t0neg0d said:
How does one handle this considering a multi-player game? No choice but to add/remove controls if new players enter/leave the game. However, I should also mention I never see this error... so it may be the ugly (but effective) way I am adding/removing these?


I guess it depends on your definition of "a lot". From my point of view I would say that anything that happens on user input (unless its something like holding down a key that triggers something every frame) is not "a lot". Certainly entering/leaving game would not be "a lot".

A lot is a very vague term but to me it would mean multiple times per frame, or at least multiple times a second...


As to the threading issue - just make sure you enqueue before you make the change as per the threading tutorial in the wiki and you should be fine.