I think should use FreeAndNil like the one below. The reason for that is
maybe after finally there is some code try to access List1/List2, without
set to nil, it would be extremely hard to detect this sorts of bug(Once
free, try to access). Set to nil is not a big performance downgrade after
now Intel release core 2 duo cpu. ;-) I am considering to buy one core 2 duo
cpu.;-)

  List1 := Nil;
  List2 := Nil;
  try
    List1 := TList.Create;
    List2 := TList.Create;

  finally
    FreeAndNil(List1);
    FreeAndNil(List2);
  end;

Regards
Leigh
www.smootharm.com

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf Of Dennis Chuah
Sent: Friday, 21 July 2006 2:46 p.m.
To: NZ Borland Developers Group - Delphi List
Subject: Re: [DUG] Related try..finally question



Depends on whether you are writing a client or server app.  On a client app:

List1 = Nil;
List2 = Nil;
try
  List1 = TList.Create;
  List2 = TList.Create;

finally
  List1.Free;
  List2.Free;
end;

Is sufficient.

On a server, you'd want to protect the Free with try ... except

finally
  try List1.Free; except end;
  try List2.Free; except end;
end;

This is so that if the first Free raises an exception, you don't end up with
a memory leak.  The second try except is strictly not needed, but I always
use it because at a later date, I may maintain the code and add more clean
up code after the second Free and don't want to forget to put the try except
in.

Obviously with two objects, you can use nested try finally, but if your code
creates a dozen or so objects, it is much clearer to use the above code.

----- Original Message -----
From: "Conor Boyd" <[EMAIL PROTECTED]>
To: "NZ Borland Developers Group - Delphi List" <[email protected]>
Sent: Friday, July 21, 2006 2:26 PM
Subject: [DUG] Related try..finally question


> I'm getting the impression here that a lot of people only have a 1:1
> relationship between try.finallys and procedures/functions?
>
> Sounds a bit dangerous.
>
> Should realistically be a try..finally for each object you instantiate
> in a method.
>
> Comments, anyone?
>
> C.
>
> _______________________________________________
> Delphi mailing list
> [email protected]
> http://ns3.123.co.nz/mailman/listinfo/delphi
>
_______________________________________________
Delphi mailing list
[email protected]
http://ns3.123.co.nz/mailman/listinfo/delphi


_______________________________________________
Delphi mailing list
[email protected]
http://ns3.123.co.nz/mailman/listinfo/delphi

Reply via email to