Android ArrayIndexOutOfBoundsException

Monkey Forums/Monkey Bug Reports/Android ArrayIndexOutOfBoundsException

Raph(Posted 2013) [#1]
I have a crash that occurs when the app is first launched on device (it does not seem to occur when resuming from suspend).

This crash only occurs on Android, in Debug. I have been unable to repro it in release, or in HTML5, Flash, or GLFW on Windows.

Setup:
- Monkey 70g
- Win7
- Nexus 7

Here are multiple crash logs. As you will see, the crash happens in different places at different times. I've had it happen in DeviceHeight(), in DrawImageRect(), in DrawImage()... but it always ends up this array exception.







[codebox]

E/AndroidRuntime(24118): FATAL EXCEPTION: GLThread 598

E/AndroidRuntime(24118): java.lang.ArrayIndexOutOfBoundsException: length=9; index=9

E/AndroidRuntime(24118): at java.util.Vector.arrayIndexOutOfBoundsException(Vector.java:907)

E/AndroidRuntime(24118): at java.util.Vector.remove(Vector.java:706)

E/AndroidRuntime(24118): at com.fracchronicles.bb_std_lang.popErr(MonkeyGame.java:113)

E/AndroidRuntime(24118): at com.fracchronicles.c_BitmapFont.p_Kerning(MonkeyGame.java:10162)

E/AndroidRuntime(24118): at com.fracchronicles.c_BitmapFont.p_GetTxtWidth(MonkeyGame.java:10192)

E/AndroidRuntime(24118): at com.fracchronicles.c_BitmapFont.p_GetTxtWidth2(MonkeyGame.java:10231)

E/AndroidRuntime(24118): at com.fracchronicles.bb_challengergui.g_CHGUI_DrawButton(MonkeyGame.java:11786)

E/AndroidRuntime(24118): at com.fracchronicles.bb_challengergui.g_CHGUI_DrawContents(MonkeyGame.java:12966)

E/AndroidRuntime(24118): at com.fracchronicles.bb_challengergui.g_CHGUI_Draw(MonkeyGame.java:13235)

E/AndroidRuntime(24118): at com.fracchronicles.c_MyGame.p_renderGUI(MonkeyGame.java:3062)

E/AndroidRuntime(24118): at com.fracchronicles.c_MyGame.p_OnRender(MonkeyGame.java:4899)

E/AndroidRuntime(24118): at com.fracchronicles.c_GameDelegate.RenderGame(MonkeyGame.java:9062)

E/AndroidRuntime(24118): at com.fracchronicles.BBGame.RenderGame(MonkeyGame.java:572)

E/AndroidRuntime(24118): at com.fracchronicles.BBAndroidGame.onDrawFrame(MonkeyGame.java:1267)

E/AndroidRuntime(24118): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1516)

E/AndroidRuntime(24118): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
[codebox]


Raph(Posted 2013) [#2]
I forgot to note -- this crash is erratic. It happens maybe 25% of the time.


ziggy(Posted 2013) [#3]
it seems to be related challenger gui measuring some text. just in case, be sure to use latest fontmachine version and if you manage to get a simple runable example maybe the challenger gui dev can fix it?


Raph(Posted 2013) [#4]
I grabbed the latest Fontmachine, just in case. Crash still happens, though again, only some of the time.

Again: only on Android, not on anything else.

Here's a fresh crash log. It was in DeviceHeight() this time:



The Monkey code:



Though I note that Monkey says the error is at the noted line but Java says it's the next one, so maybe you're right and it's actually the GUI?


muddy_shoes(Posted 2013) [#5]
As I pointed out in your original thread, the exceptions are coming from within the Vector remove call done in the popErr Monkey function. PopErr is a function that the Monkey translator adds to code in debug mode in order to provide Monkey line information in error messages.


marksibly(Posted 2013) [#6]
Can you please post something complete that I can run to reproduce the problem? The smaller the better!


Raph(Posted 2013) [#7]
Yeah, working on the surgery now :)


Raph(Posted 2013) [#8]
Just had a completely different stack trace this time, so I thought I'd add it to the thread. All I did was copy the project to a new folder to start removing parts form it for the small test case. It was different enough that I thought it might be a clue.

OK, back to trying to make a cut down repro case.




marksibly(Posted 2013) [#9]
Are you using anything related to threading, such as async image loading? A 3rd party module?


Raph(Posted 2013) [#10]
I am not using threading in my code.

3rd party modules: I am using challengergui and fontmachine. There are no references to "thread" in any of the source files for those.

I AM getting this in the debug log:

E/MP3Extractor( 128): Unable to resync. Signalling end of stream.

It spams it when the app starts up. I just commented out playing the music, and it still happens.

Additional data:

First run of the app is fine, shows me the loading screen then the title screen.

I tap the home button. My logging shows I hit OnSuspend, saved the game.

Second run of the app shows me the loading screen again, like the app exited entirely. Then it crashes. Logging shows the app went through OnCreate again, NOT OnResume.

Third run of the app shows the me the loading screen and is fine. We go through OnCreate. I tap home to exit.

After that, all launches are RESUMES of the app. I see the log line for OnResume instead of OnCreate. And it all works flawlessly.

So basically, the crash happens when the game is doing OnCreate on the app that was suspended.

I have now repro'ed it this way with logging five times in a row.

Sometimes after the crash the app seems to spontaneously restart. (?)


Raph(Posted 2013) [#11]
I have to go to dinner -- my kids are pestering! But one more tidbit:

- Commenting out PlaySound did not affect the MP3 extractor line.

- Commenting out LoadSound solved the MP3 extractor issue. The crash happens anyway.

- Same pattern, it looks like this:

Run 1:
OnCreate()
OnSuspend() when I tap home

Run 2:
OnCreate() NOT OnResume()
Crash happens here, and where varies
OnSuspend() is logged along with the exception

Run 3:
OnCreate()
OnSuspend() when I tap home

Run 4 and later:
OnResume() when I launch the app, NOT OnCreate()
OnSuspend()

Asset loading in a thread?


Raph(Posted 2013) [#12]
OK, I can get this to crash reliably. There's nothing in it but a loading screen that doesn't load anything. It works on other targets.

Repro steps:
- Android, run on device, debug
- Let it run, and close it with the home button as soon as you see "done loading" in the corner (means it got to the main loop)
- launch it again on the device. Wait a few frames. (Here it seems to crash at OnUpdate # 49 or so)

Code:



Full crash log with the debug traces:

RUN ONE (ended with tapping Home button)


RUN TWO (the crash. Note that it begins with "OnCreate" not "OnResume"). The actual stack trace is near the bottom.




marksibly(Posted 2013) [#13]
Okay, this has something to do with the way the app is started.

If you use F5 (build and run) to start an app, for some reason when you suspend/resume an app, it restarts in onCreate when resumed, leading to the above error.

However, if you use F7 (just build) and manually start the app on the device yourself, all works as expected. Note you can use 'adb logcat' from a terminal to watch app output if you run it manually.

I have no idea why this is exactly (but do remember noticing it before) and will look into it very soon (it should work either way, even if app is restarted via onCreate), but it shouldn't affect end users of your app who will generally be installing/running it manually.


marksibly(Posted 2013) [#14]
Adding this to the app manifest (inside the activity block) fixes it too:

android:launchMode="singleTop"

Updated manifest file is here:

https://github.com/blitz-research/monkey/blob/develop/targets/android/template/templates/AndroidManifest.xml

Copy this to your Monkey /targets/android/template/templates dir, delete your .build folder and try again!


Raph(Posted 2013) [#15]
That did the trick! Thanks, Mark!