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

Marco van de Voort marcov at stack.nl
Tue Jul 6 18:39:24 CEST 2010


In our previous episode, Andrew Brunner said:
> > 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.

Nope. That's why it is free and not destroy.

>  If you used FreeAndNil(Obj1) and FreeAndNil(Obj2) that might work
> around that one issue..

Doesn't matter either.

> 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 :-)

That's the problematic one I think. 



More information about the fpc-pascal mailing list