[fpc-pascal] methods of an object to create others objects
Andrew Brunner
andrew.t.brunner at gmail.com
Tue Jul 6 17:10:36 CEST 2010
>> obj1:=TMyObject.Create;
>> Try
>> obj2:=TMyObject.Create;
>> Try
>> Obj1.DoSomething1;
>> Obj2.DoSomething2;
>> Finally
>> FreeAndNil(Obj2);
>> end;
>> Finally
>> FreeAndNil(Obj1);
>> end;
>
> Hum... I do not agree. Why not this? See..
>
> obj1:=TMyObject.Create;
> obj2:=TMyObject.Create;
> Try
> Obj1.DoSomething1;
> Obj2.DoSomething2;
> Finally
> FreeAndNil(Obj1);
> FreeAndNil(Obj2);
> end;
>
In your example, if obj2:=TMyObject.Create fails (which will happen if
resources are not available)... then the exception is raised and Obj1
never gets freed. So in an example where this method is re-entrant,
you would see many instances of this occurring.
Nested exception handling is the only way to avoid memory from leaking.
More information about the fpc-pascal
mailing list