我希望有一个带接口的特殊记录。
并且,该接口具有子接口和一些类。因此,需要自动释放。 但是,在记录中的接口已经被释放了。
需要帮助,为什么引用计数不匹配?
我尝试下面的代码...
//--------------------------------------------------------------------
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;
test1
中有错误的终结代码。无法提供更多信息,需要时间进行调试。 - kludg