Home Demos Forum Research Guide
Viewing topic in forum 'general'
back to topic list

Index out of bounds exception
Hi,

I have the following situation:


    Metric m = new Metric();
    metrics = new Metric[](metrics, new Metric());
    metrics[metrics.arrayLength-1] = m;
 


What I'm trying to do is to add items to an array in a dynamic way. So I would add a new Metric object (a blank object) to the array and then I would place 'm' on the blank object's position (the last position).

The logic looks ok for me. Is there a reason why it, sometimes, would give me an array index out of bounds exception? I'm guessing it has something to do with the DXU.o component and the pseudo-asynchronous tasks it performs (if I understand it correctly). Probably my assumption is way off.

Thanks,

- Roberto
by roberto on 2015-03-24 16:54:49 (4 replies)

Hi, this looks like a bug - could you also please post the Metric data type definition so that we can recreate it exactly?

Thanks,

- Barry
by barry on 2015-03-24 17:33:45
Here it is.


data Metric {
    char name[];
    int value;
    bool high;

    DateTime timeStamp;
}

//full code
component provides HTTPHandler, monitoring.MetricMonitor, monitoring.EventMonitor requires io.Output out, data.IntUtil iu, io.FileSystem fileSystem, io.File, data.StringUtil stringUtil, time.Timer t, http.CacheHandler ch, time.Calendar ic {

        void addMetric(char name[], int value, bool high) {
            Metric m = new Metric();
            m.name = name;
            m.value = value;
            m.high = high;
            m.timeStamp = ic.getTime();

            metrics = new Metric[](metrics, new Metric());
            metrics[metrics.arrayLength-1] = m;
        }
}
 

by roberto on 2015-03-24 17:37:18
Hi Roberto,

I can't seem to reproduce this in a standalone program - is it possible that it's a concurrency control issue in your code?

Do you have another thread that's clearing the array periodically (i.e. setting it to null) in such a way that the array might get cleared immediately after the line:

metrics = new Metric[](metrics, new Metric());


But before the line:

metrics[metrics.arrayLength-1] = m;


?

- Barry
by barry on 2015-03-24 22:02:46
Yes. Indeed. You're right. My bad.

I have a thread that collects the metrics every 10 seconds, and then it deletes the entire array, setting it to null. Since the metrics are added in any given time, it might be adding a metric and at the same time deleting the entire array causing the error.

I will take care of it, and I'll report back with more details.

Thank you.
by roberto on 2015-03-25 08:32:17
© Francis Research Ltd. 2017