Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2009-01

Possible bug passing a TStringList to a function.

4 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Dave Coventry

Hi,

If I pass a TStringList to a function:

function TSLFunctn(myTSL: TSltringList):integer;
var i: integer;
begin
Result:=0;
for i:=0 to myTSL.Count-1 do //<-----------fails here
begin
Result+=strtoint(myTSL.Strings[i]);
end;
end;

The function fails when I try to assign quantify myTSL.Count.

However, this works:

function TSLFunctn(myTSL: TSltringList):integer;
var i: integer;
internalTSL: TStringList;
begin
internalTSL:= TStringList.Create;
internalTSL:=myTSL;
Result:=0;
for i:=0 to internalTSL.Count-1 do //<-----------works
begin
Result+=strtoint(internalTSL.Strings[i]);
end;
internalTSL.Free;
end;

Is this a bug? Or is that the way it's supposed to behave?

Reply by email

Luca Olivetti
to Dave Coventry

En/na Dave Coventry ha escrit:

Show quoted text 15 lines
Hi, If I pass a TStringList to a function: function TSLFunctn(myTSL: TSltringList):integer; var i: integer; begin Result:=0; for i:=0 to myTSL.Count-1 do //<-----------fails here begin Result+=strtoint(myTSL.Strings[i]); end; end; The function fails when I try to assign quantify myTSL.Count.

works here (without the typo)

Show quoted text 9 lines
However, this works: function TSLFunctn(myTSL: TSltringList):integer; var i: integer; internalTSL: TStringList; begin internalTSL:= TStringList.Create; internalTSL:=myTSL;

this is:

1) a memory leak (you create a stringlist then ignore it, losing the pointer)
2) exactly the same as before (a stringlist variable, as any other class, is actually a pointer, so if you use := both are pointing at the same object, if you want to make a copy you have to use assign, provided that the class implements it correctly)

Show quoted text 6 lines
Result:=0; for i:=0 to internalTSL.Count-1 do //<-----------works begin Result+=strtoint(internalTSL.Strings[i]); end; internalTSL.Free;

and here you are freeing the original stringlist, not the locally created one.

Bye

Luca Olivetti Wetron Automatizaci?n S.A. http://www.wetron.es/ Tel. +34 93 5883004 Fax +34 93 5883007

Reply by email

Darmawan Sugiarto

It's work at my computer
I used WinXP SP3 and Lazarus 0.9.26 beta
This my code... do you miss something ?

function TSLFunctn(myTSL: TStringList):integer;
var i: integer;
begin
?Result:=0;
? for i:=0 to myTSL.Count-1 do //<-----------fails here
? begin
? Result+=strtoint(myTSL.Strings[i]);
? end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
?? a:TStringList;
begin
?? a:=TStringList.Create;
?? a.Add('1');
?? a.Add('2');
?? ShowMessage(inttostr(TSLFunctn(a)));
end;

=============INTOSOFT============

ALEXIO CHAOS

Yahoo Messager

=================================

--- On Tue, 1/20/09, Luca Olivetti wrote:

Earlier message 60 lines
From: Luca Olivetti Subject: Re: [Lazarus] Possible bug passing a TStringList to a function. To: "General mailing list" Date: Tuesday, January 20, 2009, 12:02 AM En/na Dave Coventry ha escrit: > Hi, > > If I pass a TStringList to a function: > > function TSLFunctn(myTSL: TSltringList):integer; > var i: integer; > begin > Result:=0; > for i:=0 to myTSL.Count-1 do //<-----------fails here > begin > Result+=strtoint(myTSL.Strings[i]); > end; > end; > > The function fails when I try to assign quantify myTSL.Count. works here (without the typo) > > However, this works: > > function TSLFunctn(myTSL: TSltringList):integer; > var i: integer; > internalTSL: TStringList; > begin > internalTSL:= TStringList.Create; > internalTSL:=myTSL; this is: 1) a memory leak (you create a stringlist then ignore it, losing the pointer) 2) exactly the same as before (a stringlist variable, as any other class, is actually a pointer, so if you use := both are pointing at the same object, if you want to make a copy you have to use assign, provided that the class implements it correctly) > Result:=0; > for i:=0 to internalTSL.Count-1 do //<-----------works > begin > Result+=strtoint(internalTSL.Strings[i]); > end; > internalTSL.Free; and here you are freeing the original stringlist, not the locally created one. Bye -- Luca Olivetti Wetron Automatizaci?n S.A. http://www.wetron.es/ Tel. +34 93 5883004? ? ? Fax +34 93 5883007

Reply by email

Dave Coventry
to Darmawan Sugiarto

Darmawan,

When I put your code into my machine, it works fine.

Odd, because it is in more than one place where I've used the same technique and they all fail at the same place.

But at least it would appear to be a problem with my coding, rather than a bug in Lazarus, and it's not too difficult to work around.

Incidently my machine is AMD64 running Ubuntu 8.10 (Intrepid) and using build v0.9.27 r17479 x86_64-linux-gtk 2 (beta)

Reply by email