[fpc-devel] Suggestion: reference counted objects
pascaldragon at googlemail.com
Mon Sep 22 07:59:13 CEST 2014
On 21.09.2014 21:09, Hans-Peter Diettrich wrote:
> Sven Barth schrieb:
>> On 20.09.2014 13:42, Sven Barth wrote:
>>> On 20.09.2014 13:11, Peter Popov wrote:
>> - to remedy this TObject is extended with non-virtual methods that
>> allow manual reference counting and would rely on the RTTI data I
>> mentioned (let's call the methods AddRef, Release, IsReferenceCounted
>> and RefCount for now, which can also be used to hook up the reference
>> counting of IUnknown interfaces);
> I'd add a _RefCount field to TObject, regardless of whether it's really
> used later; this will fix the field offset - just like the VMT reference
> is fixed in TObject, but not in Object types. This will eliminate
> problems with class helpers.
I've especially written that it's not part of *every* objects, because
people complained about the size increase for all instances.
>> make it safe:
>> === code begin ===
>> function CreateObject: TObject;
>> Result := TARCObject.Create;
>> === code end ===
> Here the compiler would always insert _AddRef, just like with
> interfaces, eventually optimized (inlined?) like:
> if Result._RefCounter <> -1 then
> Result._AddRef; //or InterlockedIncrement(Result._RefCounter);
And that's another thing: people complained about having that reference
count overhead for *all* assignments.
>> - this now only leaves the problems of cycles; take this code:
>> === code begin ===
>> TSomeClass = class(TARCObject)
>> Children: specialize TList<TSomeClass>;
>> Owner: TSomeClass;
>> constructor Create(aOwner: TSomeClass);
>> constructor TSomeClass.Create(aOwner: TSomeClass);
>> Children := specialize TList<TSomeClass>.Create;
>> Owner := aOwner;
>> if Assigned(Owner) then
> Here I'd prefer
> so that the Owner can implement any decent/appropriate child management
> under the hood.
It. Was. Just. An. Example. To. Illustrate. The. Problem!
I would implement that differently as well, but there's *no* point to do
that inside an example that's supposed to be as simple as possible!
More information about the fpc-devel