【发布时间】:2013-07-17 13:27:01
【问题描述】:
我遇到了访问冲突错误 [模块“rtl170”中地址 5005F6E 的访问冲突。使用 IInterfaceList 读取地址 0000000A]。 我的代码如下所示:
if not Assigned(FoRoutingCodes) then Exit;
for i := 0 to FoRoutingCodes.nCount - 1 do
begin
...
end;
FoRoutingCodes 的声明为:
FoRoutingCodes : IRoutingCodeList;
以及IRoutingCodeList的定义
IRoutingCodeList = interface
function GetCount: Integer;
...
property nCount: Integer read GetCount;
end;
nCount 属性的实现是:
function TRoutingCodeList.GetCount: Integer;
begin
Result := 0;
if Assigned(FoItems) then
Result := FoItems.Count; // here i get the access violation error
end;
其中 TRoutingCodeList 是 IRoutingCodeList 的实现,FoItems 声明为:
FoItems: IInterfaceList;
我已经解决了这个问题
FoItems: TList<IRoutingCode>;
而不是
FoItems: IInterfaceList;
我是 delphi 的新手,谁能帮我理解以前的方法有什么问题。 我不知道这是否相关,因为我们的产品还有很多其他变化,但是这个问题是在我们从 Delphi XE 迁移到 Delphi XE3 之后才出现的。
提前致谢。
更新:回应 Uwe Raabe
我已经改变了 FoItems 的初始化从
constructor TRoutingCodeList.Create;
begin
FoItems := TInterfaceList.Create;
end;
到
constructor TRoutingCodeList.Create;
begin
FoItems := TList<IRoutingCode>.Create;
end;
【问题讨论】:
-
你有一个 nil 取消引用。你几乎肯定会尝试做类似
x := TSomeClass(nil).SomeMember的事情。认为传递给 RTL 的指针可能为零 -
我没有从界面回退到我的代码中没有的某个类...或者我误解了你的意思?
-
Delphi 中的接口对象实现了自动引用计数的特定内存模型。您必须以一种特殊的方式来适应这一点,这可能在“TList
”中实现或不实现。在对象析构函数中设置一些日志记录,您很可能会在将对象传递到列表的那一刻或之后看到意外销毁的对象。将您的列表基于特定的界面感知列表docwiki.embarcadero.com/Libraries/XE3/en/… -
你能展示一下你是如何初始化 FoItems 的吗?
-
@pavel.lazar 然后正如我所说的使用调试 DCU 并拦截触发析构函数的原因
标签: delphi delphi-xe3