On Tue, Jul 6, 2010 at 10:31 AM, Marcos Douglas <m...@delfire.net> wrote:
> Better:
>
> obj1 := nil;
> obj2 := nil;
> Try
>  obj1 := TMyObject.Create;
>  obj2 := TMyObject.Create;
>
>  obj1.DoSomething1;
>  obj2.DoSomething2;
> finally
>  obj1.Free;
>  obj2.Free;
> end;
>
> The objectcs are protected. But is boring... :)
> Everybody codify like that, afraid if resources are not available?
>

Nope. If Obj2 failed to create you will have a problem with Obj2.Free.
 If you used FreeAndNil(Obj1) and FreeAndNil(Obj2) that might work
around that one issue... But taking the real issue to the next
level... What if something in Obj1.free raised an exception... Then
you will not free Object2.  The "best" way is with nested exception
handling.  Sorry :-)
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply via email to