我正在处理一个项目,在这个项目中我有一个如下所示的接口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;
_AddRef
和_Release
与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;
创建矩形的方法如下:
我这样做:
var
lRect: TGameRectangle;
begin
lRect := TGameRectangle.Create(self);
lRect.Parent := Layout1;
lRect.Align := TAlignLayout.alClient;
FIntObj := lRect as IBoardShape;
稍后我通过将
FIntObj
设置为nil
来释放它。在Windows上,当我执行_Release
时,引用计数为1,然后计数递减并释放对象。但在Android上运行时,当我进入_Release
时,引用计数为5(引用计数显示在__ObjRelease
内部)。由于引用计数仍然很高,因此对象无法释放。我使用基本上只有我在这里发布的代码来重新创建了一个非常简单的演示。有人能解释一下在ARC中有什么不同,导致引用计数如此之高吗?
[weak]
属性没有任何问题。 - Günther the Beautiful