[DUG] Dumb Friday Question
Tim Jarvis
Tim.Jarvis at codegear.com
Fri May 4 17:55:40 NZST 2007
Hi C(K)arl,
Sure PopulateList could be in the try finally block (probably better in
a nested except block actually), but the rest of what you say does not
make sense for this function. Perhaps you are mixing up the 2 different
functions?
I purposely didn't add exception handling to the examples, as I wanted
to more clearly illustrate the class creation point. Obviously all solid
code should also have the appropriate exception handling.
Cheers Tim.
________________________________
From: delphi-bounces at delphi.org.nz [mailto:delphi-bounces at delphi.org.nz]
On Behalf Of Karl Reynolds
Sent: Friday, 4 May 2007 3:00 PM
To: 'NZ Borland Developers Group - Delphi List'; vss at vss.co.nz
Subject: RE: [DUG] Dumb Friday Question
It isn't different, but that's because:
List := TStringList.Create;
PopulateList(List);
try
if List.Count > 0 then
begin
...do lots of stuff with the list
End;
Finally
List.Free;
End;
is not safe. You need to move the PopulateList(List) inside the
try/finally, and therein lies the problem if you had encapsulated it
together with the TStringList.Create inside a function
(GetLoadsOfStrings). If you want to do things this way, you should make
GetLoadsOfStrings be a constructor instead (obviously you would have to
subclass TStringList to achieve this).
Cheers,
Carl
________________________________
From: delphi-bounces at delphi.org.nz [mailto:delphi-bounces at delphi.org.nz]
On Behalf Of Tim Jarvis
Sent: Friday, May 04, 2007 4:30 PM
To: NZ Borland Developers Group - Delphi List; vss at vss.co.nz
Subject: RE: [DUG] Dumb Friday Question
Hi Guys,
Hope you don't mind me sticking my nose in here. Firstly I agree with
the sentiment that you should try and avoid creating objects in one
place and freeing in another, however I don't think that this situation
represents that, think of a function that returns a class as a factory
method, these obviously are quite common and not bad practice at all, in
fact a constructor is a factory type method.
Procedure DoSomthing;
Var
List : TStrings;
Begin
List := GetLoadsOfStrings;
try
if assigned(List) then
begin
...do lots of stuff with the list
End;
Finally
List.Free;
End;
End;
Is really not much different from
Procedure DoSomthing;
Var
List : TStrings;
Begin
List := TStringList.Create;
PopulateList(List);
try
if List.Count > 0 then
begin
...do lots of stuff with the list
End;
Finally
List.Free;
End;
End;
The issue is where the variable is declared, and the scope of the
ownership not the act of creation, creation is fine to delegate to a
factory type method.
Regards Tim.
________________________________
From: delphi-bounces at delphi.org.nz [mailto:delphi-bounces at delphi.org.nz]
On Behalf Of Rohit Gupta
Sent: Friday, 4 May 2007 12:22 PM
To: vss at vss.co.nz; NZ Borland Developers Group - Delphi List
Subject: Re: [DUG] Dumb Friday Question
I would go along with Robert, you should not return complex types from
functions that create them, It makes the code messy, you create it in
one place and free it in another. Any sort of code analyser would also
complain about this. You could return a string = stringlist.text and
assign it to a local stringlist.
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
--
Rohit Gupta
B.E. Elec. M.E. Mem IEEE Associate IEE
Technical Manager
Computer Fanatics Limited
Tel +64 9 4892280
Fax +64 9 4892290
Email rohit at cfl.co.nz <mailto:rohit at cfl.co.nz>
Web www.cfl.co.nz <http://www.cfl.co.nz/>
________________________________
This email and any attachments contain information, which is
confidential and may be subject to legal privilege and copyright. If you
are not the intended recipient, you must not use, distribute or copy
this email or attachments. If you have received this in error, please
notify us immediately by return email and then delete this email and any
attachments.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.adventureeducation.co.nz/pipermail/delphi/attachments/20070504/73c47cb2/attachment-0001.html
More information about the Delphi
mailing list