[fpc-devel] Suggestion: reference counted objects

Sven Barth pascaldragon at googlemail.com
Sat Sep 20 12:52:57 CEST 2014


On 20.09.2014 12:36, Hans-Peter Diettrich wrote:
> Fabrício Srdic schrieb:
>> Hello,
>>
>> In platforms with managed code (.NET, Java), objects are automatically
>> freed by the memory manager / garbage collector.
>>
>> Would not it be interesting to have a similar feature in FPC?
>
> AFAIK some Delphi XE made TObject itself managed, by reference counting.
> It would be easy to introduce the same feature in FPC, so that no
> special base class would be required. Like with extended RTTI a decision
> should be made, whether managed objects should be enabled or disabled by
> default. Afterwards automatic management can be turned on or off for
> every single class or object individually.

It's basically easy, yes, but then one has to deal with code like this:

=== code begin ===

somestrlist.AddObject('Foobar', TObject(42));

=== code end ===

And also there's the problem of cycles especially considering the LCL 
where each TControl has an owner and a list of child components.
And then there's of course the dealing with existing code:

=== code begin ===

o := TObject.Create;
try
   // something
finally
   o.Free;
end;

=== code end ===

Which could lead to some unintended side effects if "o" is passed to 
some other code which keeps the instance around and ".Free" merely 
decreases the reference count. Of course that would have been a memory 
leak before and now it's not, but nevertheless it changes behavior.

Regards,
Sven



More information about the fpc-devel mailing list