[DUG] Related try..finally question
Paul Heinz
paul at accredo.co.nz
Fri Jul 21 15:04:43 NZST 2006
Leigh wrote:
> 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;
FreeAndNil is all good for the reason you cite and I recommend it too, but
it doesn't resolve the issue Dennis is defending against. Namely that if
List1.Free causes an exception, List2.Free is never executed since Delphi
(well, Windows SEH infrastructure really) will exit this finally block
immediatelty and chain up to the next exception or finally handler in the
excepion frame chain.
TTFN,
Paul.
More information about the Delphi
mailing list