spring4d-不可能使用弱<共享< tobject>
我想创建一个连接提供商,它会发出共享的< 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论