Impossible ArrayIndexOutOfBoundsException

Can anyone see how this frustum code can cause the following exception?


java.lang.ArrayIndexOutOfBoundsException
at com.ryanm.droid.rugl.util.geom.Frustum.extractFromOGL(Frustum.java:180)
...

The offending line 180:

frustum[ 3 ][ 0 ] /= t;

The declaration of the frustum arrays:

private final float[][] frustum = new float[ 6 ][ 4 ];

The only instances of “new” in that file are the 4 at the start, so there’s no way that the arrays are being changed after construction.
To put it mildly: ???

I’ve had 5 reports of this from 3 different users. The only possible explanation that I can see someone out there has taken my (freely available) code, b0rked it, and is distributing broken apks.

Does anyone have an alternative?

I expect those users are running an older version of your application, and there is totally different code on their line 180.

I agree with Riven. If there truly was an exception then line 179 should throw it first.

Also strange it doesn’t throw at line 172 first (which has the same indices);

frustum[ 3 ][ 0 ] = clip[ 3 ] - clip[ 1 ];

I suspect Riven’s right & your user’s code is different from yours. Either get them to send you their code & disassemble it or make sure they have your latest.

I wish it were that easy. The code was lifted straight from somewhere else, java-fied and committed. I haven’t touched it since, so I don’t think there are any other versions of this class out there (at least from me). Also, the crash report was versioned :-/

The only other possibility I can think of is that the compiler you used to build the classes put in a borked line number table.
Though I’ve only ever come across this when dealing with classes that have had some kind of bytecode transformation applied to them.

private final float[][] frustum = new float[ 6 ][ 4 ];

What’s to stop one of the float[] arrays in the float[6] array being altered? So basically, not as impossible as it looks.
(Also, call me a n00b, but I didn’t even know you could declare multidimensional arrays fully instantiated like that in Java.)

Cas :slight_smile:

you really ?! :slight_smile: it is native value they dont need to be instanciated (instanciating them would look strange tab[n][m]=new float ? , you must be tired
EDIT:
!!!
or I must be tired !

tab[n]=new float[m]

sorry…

@OP:

ok, probably :

probleme come from the way java manage array with two dimension, in C/C++ they are managed as a continous memory area allocated at once, while in java it is one array (1D) , with each entry pointing to another location, this enable to use multidimensional array with different lenght at each entry for the second dimension : in java double dimension array can be NOT rectangular

what you should do is :

for (int n=0;n<4;n++)
 tab[n]=new float[6];

sorry … I never use multidimensional array in java

EDIT:
It is usually more easy to use 1D (IMO), work as in C and I found them easier (and faster too) :

float tab[]=new float[4*6];
tab[x+y*4]=... 

Hah, I just figured that out a few days ago. My reaction: o_O, followed by embarrassment.

On-topic - I don’t see anywhere in this class that could throw that exception. And since the arrays are private, it’s unlikely outside code is changing them. I hate to even suggest this, but maybe you’ve hit a bug in the VM? Seems like it’s either that, a problem with the class files, or we’re all missing something patently obvious.

this one ?

frustum[ 3 ]=new float[0] 

or rather try this :


float f[][]=new float[6][4];
f[0]=new float[0];
f[0][0]=3;

output :

[quote]Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0
[/quote]

Huh? Not seeing that anywhere in the code.

something strange is that you dont get the indice in the exception

[quote]java.lang.ArrayIndexOutOfBoundsException
[/quote]

Really creppy , even because in the previous line you access successfully this position




 >>>>>      frustum[ 3 ][ 0 ] = clip[ 3 ] - clip[ 1 ];    
                frustum[ 3 ][ 1 ] = clip[ 7 ] - clip[ 5 ];
                frustum[ 3 ][ 2 ] = clip[ 11 ] - clip[ 9 ];
                frustum[ 3 ][ 3 ] = clip[ 15 ] - clip[ 13 ];
                
                /* Normalize the result */


                t =
                                sqrt(  >>>>>frustum[ 3 ][ 0 ]<<<<<<<< * frustum[ 3 ][ 0 ] + frustum[ 3 ][ 1 ]      
                                                * frustum[ 3 ][ 1 ] + frustum[ 3 ][ 2 ] * frustum[ 3 ][ 2 ] );
               
              frustum[ 3 ][ 0 ] /= t;



Definately something has changed or the exception would be thrown on the previous lines

Thanks for the feedback: So it seems that this is at least not embarrassingly obviously my fault. The possibilities that I can see:

  • Malicious users: they’ve altered or fabricated the error reports. I’ve had reports from 3 different people, I’d hope that they’d have more to do with their lives than conspire to troll me with bizarre error reports.
  • Some error in the reporting mechanism. I can’t see it if it’s there. Anyone else?
  • VM error. I’ve encountered some worrying stuff in android before, but this is on a different level entirely…

I suppose the android mailing list would be the next step…