Board index » delphi » TStringList as Result of a function

TStringList as Result of a function

Hi,

I understand that I must free a TStringList when I had create one. But
what is about a TStringList as a result in a local function? Is the
following code correct (no ressource leak) or I'm wrong?

function StrToList(const s: string): TStringList;
var
  Result: TStrings;
  x,y: string;
begin
  Result := TStringList.Create;
  ....
   do something here with s, x, y ....
  ...
  Result.Add(s);
  Result.Add(x);
  Result.Add(y);
  ...
end;

Thank you for comments
Stefan

 

Re:TStringList as Result of a function


It's ok to return a TStringList like you did, but the caller routine
will need to free the TStringList after used.

If you call your StrToList this way

ShowMessage(IntToStr( StrToList('test string').Count ));

You will get the message with the number of lines in the string list,
but you have no references to string list to free it.

Change to

var
  SL : TStringList

SL:=StrToList('test string');
ShowMessage(IntToStr( SL.Count) ));
SL.Free;

Now you can use the returned list and free after use.

[]s
Arthur

Stefan Nawrath <nawrath.ste...@vdi.de> escreveu nas notcias de
mensagem:3BF2CF06.FFC56...@vdi.de...

Quote
> Hi,

> I understand that I must free a TStringList when I had create one. But
> what is about a TStringList as a result in a local function? Is the
> following code correct (no ressource leak) or I'm wrong?

> function StrToList(const s: string): TStringList;
> var
>   Result: TStrings;
>   x,y: string;
> begin
>   Result := TStringList.Create;
>   ....
>    do something here with s, x, y ....
>   ...
>   Result.Add(s);
>   Result.Add(x);
>   Result.Add(y);
>   ...
> end;

> Thank you for comments
> Stefan

Re:TStringList as Result of a function


Stefan,

the caller of your function has to free the stringlist after its done with
it,
otherwise you'll have a memory leak. This should answer your question.

The real problem is one of design though. It's considered bad programming to
have
the calle creating something and give the caller the responsibility to free
it.

As a rule of thumb, the procedure or function that creates also does the
free.
To apply this to your problem

procedure DoSomething;
var
    AStringList: TStringList;
begin
    AStringList := TStringList.Create;
    try
        StrToList('Your string', AStringList);
    finally
        AStringList.Free;
    end;
end;

procedure StrToList(const s: String; List: TStringList);
var
    x,y: string;
begin
  Result := TStringList.Create;
  ....
   do something here with s, x, y ....
  ...
  List.Add(s);
  List.Add(x);
  List.Add(y);
  ...
end;

HTH,

Lieven

Re:TStringList as Result of a function


On Wed, 14 Nov 2001 21:07:34 +0100, Stefan Nawrath

Quote
<nawrath.ste...@vdi.de> wrote:
>I understand that I must free a TStringList when I had create one.

Right.

Quote
>But
>what is about a TStringList as a result in a local function? Is the
>following code correct (no ressource leak) or I'm wrong?

>function StrToList(const s: string): TStringList;
>var
>  Result: TStrings;

There's no need for this.

Quote
>  x,y: string;
>begin
>  Result := TStringList.Create;
>  ....
>   do something here with s, x, y ....
>  ...
>  Result.Add(s);
>  Result.Add(x);
>  Result.Add(y);
>  ...
>end;

As others have told you, there's always a danger in using a function
that returns an object. So I would use a constructor instead. Mind:
the exactly same mistake may be done with a constructor as with a
function (just because Constructors are also functions, not
procedures), but you are less likely to fall into it.

TStringToList= class (TStringList);
  Public
    Constructor MyCreate (const s: string);
end;

Constructor TStringToList.MyCreate(const s: string);
  Var
    x,y: string;
Begin
  Inherited;

  do something here with s, x, y ....

  Self.Add(s);
  Self.Add(x);
  Self.Add(y);
End;

so then you would do:

MyList:= TStringToList.MyCreate (s);

instead of

MyList:= StrToList(const s: string);

Manuel Algora
m...@encomix.es

Re:TStringList as Result of a function


Thank you for all your comments. I will change my Code to a better design.

Stefan

Other Threads