[fpc-devel] (ref types / circles) Re: Defer keyword

Sven Barth pascaldragon at googlemail.com
Sat May 8 19:18:09 CEST 2021


Am 08.05.2021 um 18:23 schrieb Ryan Joseph via fpc-devel:
>
>> On May 8, 2021, at 7:59 AM, Sven Barth via fpc-devel <fpc-devel at lists.freepascal.org> wrote:
>>
>> It has the exact same problems that my branch had (especially the interaction of reference counted instances with non-reference counted ones).
>>
>> Using a variable/parameter/field based approach (like the idea with managed records and default fields) is the more flexible one compared to the type or instance based one and thus it's more favorable.
> I still don't understand how the record approach is that much different from a managed class type which calls the same set of management operators.  Can we make a pros-cons list to clear this up and give an example of "especially the interaction of reference counted instances with non-reference counted ones"?
>
> Here's the most recent things we brought up:
>
> - Records can't be cast in a way that would break reference counting (like a managed class being cast to TObject would).
> - Generic records would create a proliferation of new types for all classes you wanted managed, so instead of using TFPGList<TSomeObject> you're using TManagedSomeObjectList or TManaged<TFPGList<TSomeObject>>, or worse yet "specialize TManaged<specialize TFPGList<TSomeObject>>"
>
> Otherwise the same set of circular references exists but I'm not sure about your concern about mixing managed types yet.
It's not about reference counted classes vs. managed records, but about 
whether it's *per type* or *per variable*, the implementation details 
are completely irrelevant for now.

And the problems are assigning a reference counted class instance to a 
non-reference counted variable or parameter. Also casting such a 
reference counted class to a non-reference counted one (e.g. to 
TObject). Allowing these kind of operations would either need to be 
forbidden which would restrict the usability of such classes or they 
could potentially lead to memory leaks or premature freeing. Not to 
mention how calling the destructor would react if the reference count 
isn't 0.

By using a mechanism based on the variable/field/parameter type you have 
a much more fine grained control and if one leaves out the implicit 
assignment from the wrapped class type to the non-wrapped one then one 
needs to do an explicit conversion.

Regards,
Sven


More information about the fpc-devel mailing list