Recent

Author Topic: List Index (0) out of bounds  (Read 29835 times)

hac3ru

  • Full Member
  • ***
  • Posts: 113
List Index (0) out of bounds
« on: September 20, 2013, 09:08:16 am »
I get this when I try to add data to multiple charts ...
I have 3 charts. If I add data to only one of them, everything's ok. If I add data to both of them, it cracks...
Code: [Select]
List Index (0) out of boundsI am not adding data in the same time. A thread is doing the Data Acquisition if I can call it like that, and adds it to charts based on some parameters. This means that the two graphs are not updated at the same time.
Code: [Select]
if parameter = 1 then
  list1.Add(x,y)
else if Parameter = 2 then
  List2.Add(x,y);
Something like that. Any ideas why it is causing that error?

wp

  • Hero Member
  • *****
  • Posts: 13562
Re: List Index (0) out of bounds
« Reply #1 on: September 20, 2013, 11:15:22 am »
Impossible to identify your problem with this little information. Just a few ideas:
Use the debugger to identify the faulty "list". This error usually occurs when you access the index of a nondefined list item. In your case, maybe the chart source does not contain any data yet, but you are reading the first item (index 0)?
Also, since you seem to be working with threads, are you sure that you follow the rules with thread synchronization etc.?

Arthurus

  • New Member
  • *
  • Posts: 23
Re: List Index (0) out of bounds
« Reply #2 on: September 21, 2013, 04:12:51 pm »
Hello,
Happens to me too.
I still have not been solved at all, because I get the error randomly when using charts.

But I think it has something to do with the .clear method of the series.

Do you use something in your code as "LineSeries1.Clear;"?
If so, try changing it to "LineSeries1.Active: = false;" and "LineSeries1.Active: = true;"

I use Lazarus v1.1 and windows.
A greeting. And sorry for not speaking English.
-------------------------------------------
Traducido por google. Original:
Hola,
A mi también me pasa.
Todavia no lo he podido solucionar del todo, pues me sale el error al azar al usar charts.
Pero creo que tiene algo que ver con el método .clear de la serie.
¿Usas algo en tu código como "LineSeries1.Clear; " ?
Si es así, intenta cambiarlo por "LineSeries1.Active:=false;" y "LineSeries1.Active:=true;"
Un saludo.
Y perdón por no saber inglés.

hac3ru

  • Full Member
  • ***
  • Posts: 113
Re: List Index (0) out of bounds
« Reply #3 on: September 22, 2013, 04:47:33 pm »
@wp I am using the Debugger but it does not show where the error occurs from. The Call stack is all weird. Still, if you saying that I'm trying to access values that are not assigned yet, I do have an idea now...

I use a ring buffer to update the chart. I get the values from some other thread, and I store them into the buffer. I get multiple variables in an array of bytes, work with them, write them into the ring buffer and after that, I clear the graph and write the new buffer in the graph. I do
Code: [Select]
List1.Clear;
for i := 0 to RingBuffer.Size - 1 do
  List1.Add(CalculatedValue, RingBuffer.Data[i])
Where RingBuffer is
Code: [Select]
type RingBuffer = Record
  Size : integer;
  WritePoint : integer;
  ReadPoint : Integer;
  Data   : Array of Integer;
end;
What could be the problem? The graph tries to update itself right after the Clear and I try to write in the same time or what?
I use the ring Buffer because I want the chart to have the values of last X datas received from the other thread so, I write 5 datas into the ring buffer, update the chart and when the next data arrives, the oldest data in the Buffer is being overwritten...

@Arthurus, yes I am. Making it inactive will not help me. I need to clear it.

//Threads Sync and critical sections are used. I took this in consideration... It does not seem to be that...
« Last Edit: September 22, 2013, 04:50:38 pm by hac3ru »

wp

  • Hero Member
  • *****
  • Posts: 13562
Re: List Index (0) out of bounds
« Reply #4 on: September 22, 2013, 05:13:31 pm »
Without your sources it is difficult to help. Here's what I would do:
  • Instead of using a chart I'd write the ring buffer to a file for a test. If this prodecues the error the bug could be in the ring buffer logics. Is its memory allocated correctly? Are the data indices out of range?
  • For a test, try to remove the thread, create dummy data maybe by means of a button click and treat them as if they would come from the thread. If this works the error should be in the thread logics, synchronization etc.
  • All these tests could give you an idea how to condense your program into a little demo project which shows the error and which you can upload here.

Arthurus

  • New Member
  • *
  • Posts: 23
Re: List Index (0) out of bounds
« Reply #5 on: September 23, 2013, 12:13:59 am »
hac3ru:
The error is: (Because this does not work well. I think it must be a bug...)
Code: [Select]
List1.Clear;
What is List1?  TLineSeries? or TOpenHighLowCloseSeries? or.....

Replace it with one like this:

Code: [Select]
for i:=0 to Chart1.SeriesCount-1 do
  begin
    if Chart1.Series[i] is TLineSeries then TLineSeries(Chart1.Series[i]).Clear;
    if Chart1.Series[i] is TOpenHighLowCloseSeries then TOpenHighLowCloseSeries(Chart1.Series[i]).Clear;
  end;

A greeting.

wp

  • Hero Member
  • *****
  • Posts: 13562
Re: List Index (0) out of bounds
« Reply #6 on: September 23, 2013, 12:22:10 am »
Quote
What is List1?  TLineSeries? or TOpenHighLowCloseSeries? or.....
From the context I'd guess that it is a TListChartSource.

Quote
Because this does not work well. I think it must be a bug...
Could you be more specific? In my own experience with many charts, TListChartSource is working fine.

Quote
Replace it with one like this:
  if Chart1.Series[ i] is TLineSeries then TLineSeries(Chart1.Series).Clear;
  if Chart1.Series[ i] is TOpenHighLowCloseSeries then TOpenHighLowCloseSeries(Chart1.Series).Clear;
No, this is useless. "Clear" is not a virtual method, it is implemented by their ancestor TChartSeries, and in every case it just clears the internal ListSource (i.e. is doing what you are claiming above to be faulty).
« Last Edit: September 23, 2013, 12:27:59 am by wp »

hac3ru

  • Full Member
  • ***
  • Posts: 113
Re: List Index (0) out of bounds
« Reply #7 on: September 23, 2013, 08:27:50 am »
hac3ru:
The error is: (Because this does not work well. I think it must be a bug...)
Code: [Select]
List1.Clear;
What is List1?  TLineSeries? or TOpenHighLowCloseSeries? or.....

Replace it with one like this:

Code: [Select]
for i:=0 to Chart1.SeriesCount-1 do
  begin
    if Chart1.Series[i] is TLineSeries then TLineSeries(Chart1.Series[i]).Clear;
    if Chart1.Series[i] is TOpenHighLowCloseSeries then TOpenHighLowCloseSeries(Chart1.Series[i]).Clear;
  end;

A greeting.

List1 is a ListChartSource...
Ideas?

Arthurus

  • New Member
  • *
  • Posts: 23
Re: List Index (0) out of bounds
« Reply #8 on: September 23, 2013, 09:40:20 am »
In my program of charts I've spent like 2 months trying to discover the cause of this error "List index (0) out of bounds", very hard to detect.

And since I have already changed that code I have not seen that error message.

Use Win & Firebird & Lazarus-1.1-40379-fpc-2.6.1-20130224-win32

@hac3ru: I do not use ListChartSource. But I am convinced that it has something to do with .Clear

Perhaps another thing, but in my case, I've solved so... and now going well.
A greeting.
--------------------------------------------
Traslate by bing. ORIGINAL:
En mi programa de charts me he pasado como unos 2 meses intentando descubrir la causa de ese error "List index(0) out of bounds", muy dificil de detectar.
Y desde que he cambiado ese código ya no he vuelto a ver ese mensaje de error.
@hac3ru: No uso ListChartSource. Pero estoy convencido de que tiene algo que ver con .Clear
Quizá sea otra cosa, pero en mi caso, yo lo he solucionado así... y por ahora me va bien.

wp

  • Hero Member
  • *****
  • Posts: 13562
Re: List Index (0) out of bounds
« Reply #9 on: September 23, 2013, 09:23:21 pm »
Quote
I do not use ListChartSource.
Even if you don't expicitly use a ListChartSource you do use it nevertheless because the data that you "Add" to the series go into a ListChartSource.

Quote
But I am convinced that it has something to do with .Clear
You are probably accessing one of the data points after the Clear. Something like
Code: [Select]
  x := Chart1LineSeries1.ListSource.Item[0]^.x;
There are some other methods of the ListChartSource that do that, like FormatItem or FormatItemXYText. After Clear, there are no more data points, Item[0] does not exist any more, and you will get the "List index (0) out of bounds" error. Of course, you don't call this intenionally, but maybe one your event handlers does it and gets called after the Clear.

When you, as you mentioned in one of the upper postings, hide the series instead of clearing it, of course, the error will not appear since the data points are still there. I am pretty sure that you did not solve your original issue this way, let's hope that it does not come back to you.

hac3ru

  • Full Member
  • ***
  • Posts: 113
Re: List Index (0) out of bounds
« Reply #10 on: September 24, 2013, 08:27:33 am »
Okay so .... How can I fix it? How can I tell who causes the error? :D

Arthurus

  • New Member
  • *
  • Posts: 23
Re: List Index (0) out of bounds
« Reply #11 on: September 24, 2013, 05:23:36 pm »
Ooooooooh!!!!  Nooooooo!!!!!
After several weeks it has come back.
But it has taken too long

http://img9.imageshack.us/img9/3433/r9v9.jpg

I will continue investigating.


wp

  • Hero Member
  • *****
  • Posts: 13562
Re: List Index (0) out of bounds
« Reply #12 on: September 24, 2013, 05:35:25 pm »
Compile Lazarus with debug information on (Tools / Configure "Build Lazarus" / Profile to build: Debug IDE). Then Lazarus should stop at the source line, maybe in one of the TAChart units, which caused the crash. From that you can investigate backwards, for example by looking at the stack trace to see from which procedure the error line was called. Without knowing your code I can't be more specific...

Arthurus

  • New Member
  • *
  • Posts: 23
Re: List Index (0) out of bounds
« Reply #13 on: September 24, 2013, 07:53:54 pm »
I'm back to change the code to cause the error

With the debugger, I get this message:
http://img845.imageshack.us/img845/1876/pyi2.jpg

And does not lead to any line of code: the Lazarus crashes:
http://img194.imageshack.us/img194/5121/zjlw.jpg

(My computer is a Core2Duo with 4Gb ram)

CODE:
Click in DbGrid: See where it says HERE
Code: [Select]
procedure TForm1.DBGrid1CellClick(Column: TColumn);
begin
  Ref_ID:=SQLQuery1REF.AsInteger;
  BtnHoyClick(BtnHoy);

  SQLQuery2.Close;
  SQLQuery2.SQL.Text:='select * from tabla_val where ref=:REFER order by fecha;';
  SQLQuery2.ParamByName('REFER').AsInteger:=Ref_ID;
  SQLQuery2.Open;
  SQLQuery2.Last;
  ultPreu:=SQLQuery2CIERRE.AsFloat;   // ultPreu para EstudioMedias.MediaP
  if SQLQuery1CIERRE.AsFloat>SQLQuery1AYER.AsFloat then Label4.Font.Color:=clBlue
  else if SQLQuery1CIERRE.AsFloat=SQLQuery1AYER.AsFloat then Label4.Font.Color:=clBlack
  else Label4.Font.Color:=clRed;
  Label4.Caption:='C='+FormatFloat('##0.###',SQLQuery1CIERRE.AsFloat)+' ('+
                   Format('%7.3n', [SQLQuery1CIERRE.AsFloat-SQLQuery1AYER.AsFloat]) + ') '+
                   Format('%4.2n',[(SQLQuery1CIERRE.AsFloat-SQLQuery1AYER.AsFloat)/SQLQuery1AYER.AsFloat*100])+'%'+
                   '    H='+FormatFloat('##0.###',SQLQuery1HIGH.AsFloat)+'  L='+FormatFloat('##0.###',SQLQuery1LOW.AsFloat)+
                   '    ' + SQLQuery1FECHA.AsString ;
  SQLQuery2.MoveBy(NumSesiones);
  LimpiaChart;    // <----------------------------------------------- HERE --------------------------------------------------------------
  Chart1.ZoomFull;

  if SQLQuery1ENCARTERA.AsString='S' then
  begin
    chart1.BackColor:=clTeal;
    if not chart1.Visible=true then Panel6.Visible:=true;
  end
  else begin
    chart1.BackColor:=ColorFondoGrafico;//clSilver;//clBlack;
    Panel6.Visible:=false;
  end;

  if (MediaDeTabla=True) and (SQLQuery8OpcionesUSARMEDPART.AsString='S') then EditMP.Text:=IntToStr(SQLQuery1MEDIA.AsInteger);
  case Estudio of
    1:EstudioCV.pintaChart;
    2:EstudioMedias.MediaP(StrToInt(EditMP.Text)); // <---------------------------------------- HERE ----------------------------------
  end;
  LabelHi.Caption:='';LabelCi.Caption:='';LabelLo.Caption:='';
  LabelHi1.Caption:='';LabelCi1.Caption:='';LabelLo1.Caption:='';
  MediaDeTabla:=True;
end;

Clean the Chart:
Code: [Select]
procedure TForm1.LimpiaChart;
var i:integer;
begin
  {
  for i:=0 to Chart1.SeriesCount-1 do
  begin
    if Chart1.Series[i] is TLineSeries then TLineSeries(Chart1.Series[i]).Clear;
    if Chart1.Series[i] is TOpenHighLowCloseSeries then TOpenHighLowCloseSeries(Chart1.Series[i]).Clear;
  end;
  }

  Chart1hlc.Clear;
  LineS1.Clear;
  LineMediaP7Max.Clear;
  LineMediaP6Max.Clear;
  LineMediaP5Max.Clear;
  LineMediaP4Max.Clear;
  LineMediaP3Max.Clear;
  LineMediaP2Max.Clear;
  LineMediaP1Max.Clear;
  LineMediaP1.Clear;
  LineMediaP1Min.Clear;
  LineMediaP2Min.Clear;
  LineMediaP3Min.Clear;
  LineMediaP4Min.Clear;
  LineMediaP5Min.Clear;
  LineMediaP6Min.Clear;
  LineMediaP7Min.Clear;
end;

With .Active:=false; the program behaves better than .Clear;
« Last Edit: September 24, 2013, 10:55:51 pm by Arthurus »

wp

  • Hero Member
  • *****
  • Posts: 13562
Re: List Index (0) out of bounds
« Reply #14 on: September 24, 2013, 09:24:59 pm »
Thanks for showing some code. But it's still difficult to see the details. Could you write a simplified version of your application which contains the essential parts to show the bug? You could skip database access in the first try and just add random values to the series. Upload this project here, I can try to have a look at it (no guarantee, though...)

When you step into "LimpiaChart" the "Clear" of which series causes the crash?
Temporarily remove Chart1.ZoomFull in "DBGridCellClick"? Do you zoom somewhere in your code? Deactivate this (and other ChartTools) for debugging, it may have a life of its own.

Which series types are used? Do they get data from an external chart source? Do you use a DBChartSource?

You don't seem to put your code into methods of TForm1, but call non-object procedures instead. Could it be that you have a second instance of TForm1 which you intend to call, but since Form1 is hardcoded into your procedures they use Form1 instead of the other form?


 

TinyPortal © 2005-2018