【问题标题】:Access Violation error using IInterfaceList Delphi XE3使用 IInterfaceList Delphi XE3 的访问冲突错误
【发布时间】: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


【解决方案1】:

仅考虑您到目前为止发布的代码,我看不出问题所在。但是,由于您知道错误的确切位置,我的建议是在代码中的该行设置一个断点并评估 (CTRL+F7) 成员 FoItems 以查看它是什么。

通常,当我怀疑我有一个错误的对象引用时,我会评估它的 ClassName(在您的情况下,FoItems.ClassName)。如果引用无效,此方法通常会引发异常,返回一个奇怪的名称。

另外,在构造函数中设置断点以确保 FoItems 确实被实例化了。

我希望这会有所帮助!

【讨论】:

  • FoItems.ClassName 不存在,因为这个FoItems 是一个接口。这个答案应该是评论。
  • 题主说FoItems的声明被更正为TList类型,所以ClassName方法可用。
  • 不是这样。报告的错误是使用接口时。这个问题令人困惑,无法回答。
猜你喜欢
  • 2010-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多