【问题标题】:Access violation exception from Delphi's Supports -> QueryInterface来自 Delphi 的 Supports -> QueryInterface 的访问冲突异常
【发布时间】:2010-03-21 10:10:18
【问题描述】:

我有以下代码:

  for i := 0 to FControlList.Count - 1 do
      if Supports(IMyControl(FControlList[i]), IMyControlEx) then
      begin
        MyControlEx := IMyControl(FControlList[i]) as IMyControlEx;
        MyControlEx.DoYourMagic(Self, SomeData);
      end;

此代码在我的应用程序执行期间被多次调用,但在某些特定情况下,它在 Supports() 方法中失败。更具体地说 - 它似乎属于 Supports() 方法中的 QueryInterface() 调用。

我检查了 FControlList 不是 nil 并且 FControlList[i] 不是 nil 并且它仍然会发生。

任何想法将不胜感激!!!

【问题讨论】:

  • 您能否在 Supports 通话中移除对 IMyControl 的演员表?什么是 FControlList,一个 IInterfaceList?
  • 还要注意,TControl 可以有自己的生命周期管理,即使它们是 TInterfacedPersistent 的子类,它们也不使用引用计数。见stackoverflow.com/questions/2182612
  • 只是因为 TInterfacedPersistent 不使用引用计数。否则使用 TInterfacedObject。
  • 支持需要为接口定义一个 GUID,我假设你已经为 IMyControlEx 这样做了。

标签: delphi access-violation queryinterface


【解决方案1】:

即使FControlList[I] 不是nil,也不意味着它指向有效数据。底层对象实例可能已经被释放。

我还建议删除类型转换为IMyControlSupports 可以将对象和接口作为参数,即使它们是nil,并产生所需的结果。

此外,为了代码的方便性和可读性,您可以考虑使用这种调用supports的方式:

if Supports(FControlList[i], IMyControlEx, MyControlEx) then
begin
  MyControlEx.DoYourMagic(Self, SomeData);
end;

【讨论】:

  • 看起来你是对的,它确实有一些释放的数据。这个问题非常复杂,我还没有解决它,但仍然 - 这就是答案。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-01
  • 2012-11-14
  • 1970-01-01
相关资源
最近更新 更多