[Lazarus] Possible bug passing a TStringList to a function.

Darmawan Sugiarto darmawan_sugiarto at yahoo.com
Tue Jan 20 10:01:36 CET 2009


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

   Darmawan_Sugiarto at yahoo.com   

          Yahoo Messager

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

--- On Tue, 1/20/09, Luca Olivetti <luca at wetron.es> wrote:

From: Luca Olivetti <luca at wetron.es>
Subject: Re: [Lazarus] Possible bug passing a TStringList to a function.
To: "General mailing list" <lazarus at lazarus.freepascal.org>
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
_______________________________________________
Lazarus mailing list
Lazarus at lazarus.freepascal.org
http://www.lazarus.freepascal.org/mailman/listinfo/lazarus



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20090120/e5f772cc/attachment-0007.html>


More information about the Lazarus mailing list