Bessere Absicherung gegen Memory Leaks erhält man mit try .. finally Blöcken:
Delphi-Quellcode:
Object1 := TMyFirstClass.Create;
try
// use Object1
...
Object2 := TMySecondClass.Create;
try
// use Object2
...
Object3 := TMyThirdClass.Create;
try
// use Object3
...
finally
FreeAndNil(Object3);
end;
finally
FreeAndNil(Object2);
end;
finally
FreeAndNil(Object1);
end