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;
begin
  Result := TARCObject.Create;
  Result.AddRef;
end;

=== 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 ===

type
  TSomeClass = class(TARCObject)
    Children: specialize TList<TSomeClass>;
    Owner: TSomeClass;
    constructor Create(aOwner: TSomeClass);
  end;

constructor TSomeClass.Create(aOwner: TSomeClass);
begin
  Children := specialize TList<TSomeClass>.Create;
  Owner := aOwner;
  if Assigned(Owner) then
    Owner.Children.Add(Self);
end;

Here I'd prefer
   Owner.AddChild(Self);
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!

Regards,
Sven
_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply via email to