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

Sven Barth pascaldragon at googlemail.com
Sat May 8 15:59:30 CEST 2021


Am 07.05.2021 um 23:16 schrieb Ryan Joseph via fpc-devel:
>
>> On May 7, 2021, at 2:52 PM, Sven Barth <pascaldragon at googlemail.com> wrote:
>>
>> As said the main problem of reference counting on object instances, especially if enabled by default like the Delphi NextGen compiler did, will lead to problems in *existing* code and thus is a no-go.
>>
> What did you think about me other email that had ideas to add compiler directives like $M+? The way record management operators are implemented is that if you include any of the operators then the type becomes "managed" in the same way other ref counted types are handled.
>
> For classes this is different because there is a hierarchy which is now altered but the compiler could still insert a hidden super class above it and use that to store the extra data. Indeed this would mean that existing classes (like the RTL) would not be eligible for reference counting unless it was compiled using said directive.
>
> For example the follow class:
>
> {$RETAINED+}
> type
>   TMyObject = class(TBaseClass)
>   end;
> {$RETAINED-}
>
> would become:
>
> type
>   TMyObject_RefCounted = class abstract(TBaseClass)
>    strict private
>      refCount: LongInt;
>   end;
>   TMyObject = class(TMyObject_RefCounted)
>   end;
>
> and now "TMyObject" is a managed type and Initialize/Finalize/AddRef/Copy will be called. It occurs to me now though that the ref counting would be tied to the type so if you cast the class to TObject and passed it around then ref counting wouldn't happen. Not sure if that's a deal breaker or not but it could easily cause hard to fix memory leaks.... just like normal classes. :)

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.

Regards,
Sven


More information about the fpc-devel mailing list