[fpc-pascal] methods of an object to create others objects

Andrew Brunner andrew.t.brunner at gmail.com
Tue Jul 6 18:07:48 CEST 2010

On Tue, Jul 6, 2010 at 10:31 AM, Marcos Douglas <md at 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 :-)

More information about the fpc-pascal mailing list