views:

198

answers:

1

Hello all, I have an interesting problem being reported to me from an android application I have published. I have a two-dimensional array that I am iterating through using two for loops like so:

for (int i = 0; i < arr.length; ++i)
{
    for (int j = 0; j < arr[i].length; ++j)
    {
        if (arr[i][j] != 0)
            // does stuff
    }
}

The problem is, somehow arr[i][j] != 0 is throwing an ArrayIndexOutOfBoundsException. But very rarely. I have thousands of people use the app on a daily basis and get maybe twenty force close reports.

Is this something I can't avoid, maybe a problem with the phones memory, etc. or is there something I can do that I haven't thought of yet? Thanks.

+6  A: 

This exception is not impossible if you have one thread reading the arrays while a second thread is mutating them. Specifically, if the mutating thread is changing the size of array slices; e.g.

array[i] = new Whatever[array[i].length - 1];
Stephen C
This makes a lot of sense, I must have a sync leak somewhere. Thanks
Guzba