[fpc-pascal] methods of an object to create others objects
andrew.t.brunner at gmail.com
Tue Jul 6 18:42:07 CEST 2010
On Tue, Jul 6, 2010 at 11:39 AM, Marco van de Voort <marcov at stack.nl> wrote:
> 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.
Double Nope. You cannot access methods of a nil object. Nil.Free
will in-it-and-of-it cause a read access violation.
>> 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.
> fpc-pascal maillist - fpc-pascal at lists.freepascal.org
More information about the fpc-pascal