[DUG] Dumb Friday Question

Robert martin rob at wildsoft.co.nz
Fri May 4 14:02:19 NZST 2007


Hi

Your code would leak.  You are creating an object and not freeing it.  
The calling function that retrieves the result must handle the freeing 
of the object. 

It might be clearer if you just change the function to a procedure that 
takes a TStringList as a parameter.  i.e

//Replacement procedure
procedrue NewProc (sl :TStringList);
begin
    sl.Add('Blah Blah');
end;

//Calling procedure
procedure call
var
    AStringList : TStringList;
begin
    AStringList := TStringList.Create;
    try
       NewProc(AStringLIst);

       ......

    finally
       AStringList.Free;
    end;

end;

Hope that helps :)

Rob Martin
Software Engineer

phone +64 03 377 0495
fax   +64 03 377 0496
web www.chreos.com

Wild Software Ltd



Jeremy Coulter wrote:
> Hi All. This is a question that might be infulenced by some serious 
> lack of sleep :-)
>  
> I have a funtion. Its return result is a TStringlist.
> In my code I create a TStringlist then add my values to it, then pass 
> this to the RESULT varaible for the function.
>  
> Now,  this is prob. an obvious answer than I prob. do actually know, 
> but if I got:-
>  
> sResult := TStringList.create;
> sResult.add('blah');
> Result:=sResult;
>  
> Then if I free sResult, then I loss the values I added, and the result 
> is empty as you would expect.
> But the issue I have is, so if I DONT free sResults, what happens to 
> it? Surley it stays in memory,a dn I would end up with a memory leack 
> after repeaditive calls. Is that right? Or is because the variable is 
> function specific its free by default etc?
> Its a basic question I know....but the more I thought about it the 
> more uncertain I became....I really need some sleep so that prob. the 
> real probelm :-)
>  
> Jeremy
>  
>  
> ------------------------------------------------------------------------
>
> _______________________________________________
> NZ Borland Developers Group - Delphi mailing list
> Post: delphi at delphi.org.nz
> Admin: http://delphi.org.nz/mailman/listinfo/delphi
> Unsubscribe: send an email to delphi-request at delphi.org.nz with Subject: unsubscribe


More information about the Delphi mailing list