TStringList in an array in Lazarus

1k Views Asked by At

I have a problem with TStringLists in Lazarus. I have an array called 'trans' of records called 'TTrans' which contain, among other things, TStringList called 'meebetalers'. So when I need to know for example the amount of lines in that StringList I would have to write this right? trans[i].meebetalers.Count;

Anyways, I first create a stringlist and put the selected strings from a checklistbox in it, and that works (i.e. the program returns 3 when I ask for the Count, which is correct).

In this piece of code I add values to the StringList:

slmeebetalers := TStringList.Create;
for i:= 0 to Form6.CLBox.Count-1 do begin
        if Form6.CLBox.Checked[i] then begin
          slmeebetalers.Add(Form6.CLBox.Items[i]);
          end;
        end;

Then I put the stringlist a procedure, and in that procedure I assign my first created StringList to the stringlist I mentionned before (trans[i].meebetalers), see my piece of code next.

Unit6.VoegTransToe(Form6.TransNaam.Text,
                         Form6.TrComboBox.Text,
                         bedrag,
                         slmeebetalers,
                         Form6.CalendarDialog1.Date);

But when I then ask for the count, it returns 0.

procedure VoegTransToe(naam, betaalpers: string; bedrag: currency;
  meebetalers: TStringList; datum: TDateTime);
begin
  aantaltrans:= aantaltrans+1;
  trans[aantaltrans].naam:=naam;
  trans[aantaltrans].pers.naam:=betaalpers;
  trans[aantaltrans].bedrag:=bedrag;
  trans[aantaltrans].datum:=datum;
  meebetalers:= TStringList.Create;

    trans[aantaltrans].meebetalers:= TStringList.Create;
    trans[aantaltrans].meebetalers.Assign(meebetalers);
  meebetalers.Free;
  //trans[aantaltrans].meebetalers.Free;
end; 

note The difference in name of the variable is because they are in different units

With this code I don't get an error, but it returns 0. When I say //meebetalers.Free; the same happens. But when I add //trans[aantaltrans].meebetalers.Free; I don't get an error while compiling, but when I call the procedure. Then I get this error: Project project1 raised exception class 'External: SIGSEGV'.

I think there is something wrong with the Create and Free function, but I don't know what. When I implement the try...finally...end it returns the same error. Can anybody help me?

1

There are 1 best solutions below

0
Remy Lebeau On

The problem is that your VoegTransToe() procedure is ignoring the populated TStringList object that is passed in via its meebetalers parameter. You are resetting meebetalers to point at a newly created empty TStringList object just before assigning meebetalers to trans[aantaltrans].meebetalers.

procedure VoegTransToe(naam, betaalpers: string; bedrag: currency;
  meebetalers: TStringList; datum: TDateTime);
begin
  aantaltrans:= aantaltrans+1;
  trans[aantaltrans].naam:=naam;
  trans[aantaltrans].pers.naam:=betaalpers;
  trans[aantaltrans].bedrag:=bedrag;
  trans[aantaltrans].datum:=datum;
  // meebetalers:= TStringList.Create; // <-- GET RID OF THIS!

  trans[aantaltrans].meebetalers:= TStringList.Create;
  trans[aantaltrans].meebetalers.Assign(meebetalers);
  //meebetalers.Free; // <-- AND THIS!
end; 

Don't forget to Free() the input TStringList object when you are done using it:

slmeebetalers := TStringList.Create;
try
  for i := 0 to Form6.CLBox.Count-1 do begin
    if Form6.CLBox.Checked[i] then begin
      slmeebetalers.Add(Form6.CLBox.Items[i]);
    end;
  end;
  Unit6.VoegTransToe(..., slmeebetalers, ...);
finally
  slmeebetalers.Free;
end;