gpt4 book ai didi

delphi 2009,界面已经发布

转载 作者:行者123 更新时间:2023-12-03 15:23:33 26 4
gpt4 key购买 nike

我想要有接口(interface)的特殊记录。

并且,该接口(interface)有子接口(interface)和一些类。所以,需要自动释放。不过,备案中的接口(interface)已经发布了。

需要帮助,为什么引用计数不匹配?

我尝试下一个代码...

//-------------------------------------------------------- -----------------------

type
IIn = interface
procedure SetValue(v : string);
function AsString() : string;
function GetChild() : IIn;
end;

RIn = record
FIn : IIn;

procedure SetInterface(intf : IIn);
procedure SetValue(v : string);
function AsString() : string;
function GetChild() : RIn;
end;

TIn = class(TInterfacedObject, IIn)
private
FChild : IIn;
FValue : string;
public
procedure SetValue(v : string);
function AsString() : string;
function GetChild() : IIn;
end;

//-------------------------------------------------------- -----------------------

procedure RIn.SetInterface(intf : IIn);
begin
FIn := intf;
end;

function RIn.GetChild() : RIn;
var
childInterface : IIn;
begin
if FIn = nil then FIn := TIn.Create();
childInterface := FIn.GetChild();

Result.SetInterface( childInterface );
end;

procedure RIn.SetValue(v : string);
begin
if FIn = nil then FIn := TIn.Create();
FIn.SetValue(v);
end;

function RIn.AsString() : string;
begin
if FIn = nil then FIn := TIn.Create();

Result := FIn.AsString();
end;

function RIn.GetRefCnt() : integer;
begin
if FIn = nil then FIn := TIn.Create();

Result := FIn.GetRefCnt();
end;

procedure TIn.SetValue(v : string);
begin
FValue := v;
end;

function TIn.AsString() : string;
begin
Result := FValue;
end;

function TIn.GetChild() : IIn;
begin
if FChild = nil then FChild := TIn.Create();

Result := FChild;
end;

//-------------------------------------------------------- -----------------------

// global var
var
test : RIn;

// test procedure 1
procedure test1;
begin
test.GetChild().SetValue('test...');
end;

// test procedure 2
procedure test2;
begin
ShowMessage( test.GetChild().AsString ); <----- Error!! child interface is already released..
end;

最佳答案

这是 Delphi 2009 引用计数错误。我稍微修改了您的代码以输出引用计数器:

program Bug2009;

{$APPTYPE CONSOLE}

uses
SysUtils;

type
IIn = interface
procedure SetValue(v : string);
function AsString() : string;
function GetChild() : IIn;
end;

RIn = record
FIn : IIn;

procedure SetInterface(intf : IIn);
procedure SetValue(v : string);
function AsString() : string;
function GetChild() : RIn;
end;

TIn = class(TInterfacedObject, IIn)
private
FChild : IIn;
FValue : string;
public
procedure SetValue(v : string);
function AsString() : string;
function GetChild() : IIn;
end;

procedure RIn.SetInterface(intf : IIn);
begin
FIn := intf;
end;

function RIn.GetChild() : RIn;
var
childInterface : IIn;
begin
if FIn = nil then FIn := TIn.Create();
childInterface := FIn.GetChild();
Result.SetInterface( childInterface );
end;

procedure RIn.SetValue(v : string);
begin
if FIn = nil then FIn := TIn.Create();
FIn.SetValue(v);
end;

function RIn.AsString() : string;
begin
if FIn = nil then FIn := TIn.Create();

Result := FIn.AsString();
end;

procedure TIn.SetValue(v : string);
begin
FValue := v;
end;

function TIn.AsString() : string;
begin
Result := FValue;
end;

function TIn.GetChild() : IIn;
begin
if FChild = nil then FChild := TIn.Create();
Writeln(FChild._AddRef - 1);
FChild._Release;
Result := FChild;
end;

// global var
var
test : RIn;

// test procedure 1
procedure test1;
begin
test.GetChild().SetValue('test...');
end;

// test procedure 2
procedure test2;
begin
Writeln( test.GetChild().AsString ); // <----- Error!! child interface is already released..
end;

begin
try
test1;
test2;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
readln;
end.

输出(Delphi 2009)是

Bug2009

对 Delphi XE 输出进行相同的测试

No bug Delphi XE

查看不同的引用计数器值

关于delphi 2009,界面已经发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18168402/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com