[Lazarus] Possible bug passing a TStringList to a function.
Luca Olivetti
luca at wetron.es
Tue Jan 20 09:02:57 CET 2009
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
More information about the Lazarus
mailing list