>> 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.
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply via email to