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

Ryan Joseph genericptr at gmail.com
Fri May 7 23:16:58 CEST 2021



> 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. :)

Regards,
	Ryan Joseph



More information about the fpc-devel mailing list