Releasing a conjugated object using ARC vs without - Delphi

I am working on a project where I have a conjugate TRectangle:

IBoardShape = interface(IInterface)
  function GetColor: integer;
  procedure SetColor(const aColor: integer);
  property Color: integer read GetColor write SetColor;
end;

TGameRectangle = class(TRectangle, IBoardShape)
private
  FColor: integer;
  procedure SetColor(const aColor: integer);
  function GetColor: integer;
  property Color: integer read GetColor write SetColor;
protected
  {$IFNDEF AUTOREFCOUNT}
  [Volatile] FRefCount: Integer;
  {$ENDIF}
  function _AddRef: Integer; stdcall;
  function _Release: Integer; stdcall;
end;

_AddRefand the _Releasesame as in InterfacedObject:

function TGameRectangle._AddRef: Integer;
begin
{$IFNDEF AUTOREFCOUNT}
  Result := AtomicIncrement(FRefCount);
{$ELSE}
  Result := __ObjAddRef;
{$ENDIF}
end;

function TGameRectangle._Release: Integer;
begin
{$IFNDEF AUTOREFCOUNT}
  Result := AtomicDecrement(FRefCount);
  if Result = 0 then
    Destroy;
{$ELSE}
  Result := __ObjRelease;
{$ENDIF}
end;

To create a rectangle, I do this:

var  
  lRect: TGameRectangle;
begin
  lRect := TGameRectangle.Create(self);
  lRect.Parent := Layout1;
  lRect.Align := TAlignLayout.alClient;
  FIntObj := lRect as IBoardShape;

I will free it later by setting FIntObjto nil. On Windows, when I follow _Release, the reference count is 1, and the counter decreases and the object is freed. When running on Android, the number of links is 5 when I enter _Release(the link count is displayed inside __ObjRelease). Since the reference count is still high, the object is not freed.

I recreated this in a very simple demo, using mostly just the code that I posted here. Can someone explain what is different in ARC, which makes the link count so high?

+3
1

ARC, ARC .

ARC IInterface .

ARC :

TGameRectangle = class(TRectangle, IBoardShape)
private
  FColor: integer;
  procedure SetColor(const aColor: integer);
  function GetColor: integer;
  property Color: integer read GetColor write SetColor;
end;

, , ARC. , TComponent, . , , . , . , . .

, , ARC, , . .

+1

All Articles