spring4d-不可能使用弱<共享< tobject>

发布于 2025-01-20 18:39:30 字数 1766 浏览 0 评论 0原文

我想创建一个连接提供商,它会发出共享的< t>并坚持一个弱者< t>但是,即使仅创建了1个实例,但仍有2种销毁的尝试。

我在下面包括一个基本示例。

  TMyObj = class
  private
    class var
      fNextId: Integer;
    var
      fId: Integer;
  public
    constructor Create;
    destructor Destroy; override;
  end;

  IProv = interface
  ['{0497DFE9-A77C-4250-82AF-FDA3823A91EC}']
    function Get: Shared<TMyObj>;
    function GetNew: Shared<TMyObj>;
  end;

  TProv = class(TInterfacedObject, IProv)
  private
    fInstance: Weak<TMyObj>;
  protected
    function Get: Shared<TMyObj>;
    function GetNew: Shared<TMyObj>;
  end;

  THolder = class
  private
    fO: Shared<TMyObj>;
  public
    constructor Create(O: Shared<TMyObj>);
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
  var P:=TProv.Create as IProv;

  var H:=THolder.Create(P.Get);
  var H2:=THolder.Create(P.Get);
  FreeAndNil(H);
  FreeAndNil(H2);
  OutputDebugString('End');
end;

{ TMyObj }

constructor TMyObj.Create;
begin
  fId:=InterlockedIncrement(fNextId);
  OutputDebugString(PChar(fId.ToString+' Creating On '+GetCurrentThreadId.ToString));
end;

destructor TMyObj.Destroy;
begin
  OutputDebugString(PChar(fId.ToString+' Destroying On '+GetCurrentThreadId.ToString));
  inherited;
end;

{ TProv }

function TProv.Get: Shared<TMyObj>;
begin
  if fInstance.IsAlive then
    Exit(fInstance.Target);

  Result:=GetNew;
  fInstance:=Result;
end;

function TProv.GetNew: Shared<TMyObj>;
begin
  Result:=TMyObj.Create;
end;

{ THolder }

constructor THolder.Create(O: Shared<TMyObj>);
begin
  fO:=O;
end;

然后,我认为那个弱&lt; t&gt;仅适用于界面,因此更改了实现,以保持弱点; iShared&lt; t&gt;&gt;但是,当我称弱的词时,false返回。

I want to create a connection provider which issues Shared<T> and holds on to a Weak<T> but even though only 1 T instance gets created there are 2 attempts to destroy.

I have included a basic example below.

  TMyObj = class
  private
    class var
      fNextId: Integer;
    var
      fId: Integer;
  public
    constructor Create;
    destructor Destroy; override;
  end;

  IProv = interface
  ['{0497DFE9-A77C-4250-82AF-FDA3823A91EC}']
    function Get: Shared<TMyObj>;
    function GetNew: Shared<TMyObj>;
  end;

  TProv = class(TInterfacedObject, IProv)
  private
    fInstance: Weak<TMyObj>;
  protected
    function Get: Shared<TMyObj>;
    function GetNew: Shared<TMyObj>;
  end;

  THolder = class
  private
    fO: Shared<TMyObj>;
  public
    constructor Create(O: Shared<TMyObj>);
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
  var P:=TProv.Create as IProv;

  var H:=THolder.Create(P.Get);
  var H2:=THolder.Create(P.Get);
  FreeAndNil(H);
  FreeAndNil(H2);
  OutputDebugString('End');
end;

{ TMyObj }

constructor TMyObj.Create;
begin
  fId:=InterlockedIncrement(fNextId);
  OutputDebugString(PChar(fId.ToString+' Creating On '+GetCurrentThreadId.ToString));
end;

destructor TMyObj.Destroy;
begin
  OutputDebugString(PChar(fId.ToString+' Destroying On '+GetCurrentThreadId.ToString));
  inherited;
end;

{ TProv }

function TProv.Get: Shared<TMyObj>;
begin
  if fInstance.IsAlive then
    Exit(fInstance.Target);

  Result:=GetNew;
  fInstance:=Result;
end;

function TProv.GetNew: Shared<TMyObj>;
begin
  Result:=TMyObj.Create;
end;

{ THolder }

constructor THolder.Create(O: Shared<TMyObj>);
begin
  fO:=O;
end;

I then thought that Weak<T> would only work for interfaces so changed the implementation to hold a Weak<IShared<T>> but then false is returned when I call Weak.IsAlive.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文