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

Marcos Douglas md at delfire.net
Tue Jul 6 17:31:59 CEST 2010


On Tue, Jul 6, 2010 at 12:10 PM, Andrew Brunner
<andrew.t.brunner at gmail.com> wrote:
>> 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.

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?


Marcos Douglas



More information about the fpc-pascal mailing list