我工作的一個項目,我有一個接口TRectangle這樣的:解放出來與ARC的接口對象VS無 - 德爾福
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
(引用計數顯示在__ObjRelease
的內部)時,引用計數爲5。由於引用計數仍然很高,該對象不會釋放。
我在一個非常簡單的演示中使用基本上只是我在這裏發佈的代碼重新創建了這個。有人可以解釋一下ARC的不同之處在於導致引用計數如此之高?
使用ARC **時,每個對該實例的引用都會增加RefCount。您傳遞的是一個所有者和一個父項,並且它們都持有**最少**一個該實例的引用。加上你的界面引用你至少有3個 - 但肯定有更多的引用是由所有者和父引起的。 –
@SirRufo ...除非您使用[[weak]]參考,這是必需的,例如爲循環引用。參見[Marco的文章](http://blog.marcocantu.com/blog/automatic_reference_counting_for_delphi.html)。恕我直言,德爾福內存模型的這種重大變化是有史以來最糟糕的想法之一。 –
@ArnaudBouchez德爾福太害怕改變,特別是突破變化。我渴望臺式機編譯器ARC的出現。使用'[weak]'屬性沒有任何問題。 –