小编Dav*_*son的帖子

Delphi"支持"增加[弱]或[不安全]接口的引用计数

当我使用Delphi Berlin 10.1 [weak](和[unsafe])引用时,"Supports"函数和"QueryInterface"都在给定一个标有"weak"属性的接口变量时递增引用计数(与"with"相同的行为)不安全的"属性".

program WeakReferences;

{$APPTYPE CONSOLE}

{$R *.res}

uses System.SysUtils;

type
   IAnInterfacedObject = interface
   ['{351DFDA3-42CA-4A1D-8488-494CA454FD9C}']
   end;

   TAnInterfacedObject = class(TInterfacedObject, IAnInterfacedObject)
     protected

      function  GetTheReferenceCount : integer;

     public
      constructor Create;
      destructor  Destroy; override;
      property    TheReferenceCount : integer read GetTheReferenceCount;
   end;

   constructor TAnInterfacedObject.Create;

   begin
      inherited Create;
      writeln('(create AIO instance)');
   end;

   destructor TAnInterfacedObject.Destroy;

   begin
      writeln('(destroy AIO instance)');

      inherited Destroy;
   end;

   function TAnInterfacedObject.GetTheReferenceCount : integer;

   begin
      Result := FRefCount;
   end;

   procedure WithoutSupports;

   var
      AIOinstance : TAnInterfacedObject;

      AIOinterfaced : IAnInterfacedObject;

      [Weak]
      WeakAIOinterfaced : IAnInterfacedObject; …
Run Code Online (Sandbox Code Playgroud)

delphi weak-references delphi-10.1-berlin

11
推荐指数
1
解决办法
545
查看次数