来自 Delphi 支持的访问冲突异常 ->查询接口

发布于 2024-08-26 04:12:40 字数 474 浏览 10 评论 0原文

我有以下代码:

  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 不为零并且 FControlList[i] 不为零,但它仍然发生。

任何想法将不胜感激!

I have the following piece of code:

  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;

This code is called many times during my application execution, but in some specific cases it fails inside the Supports() method. And more specifically - it seems to fall inside the QueryInterface() call within the Supports() method.

I checked that FControlList is not nil and FControlList[i] is not nil and it still happens.

Any idea will be appreciated!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

來不及說愛妳 2024-09-02 04:12:40

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

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

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

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

Even if FControlList[I] is not nil, that doesn't mean it points to valid data. The underlying object instance may have been freed already.

I also suggest to remove the type cast to IMyControl. Supports can take objects and interfaces as parameter, even if they are nil, and produce the desired result.

In addition you may consider using this way of calling supports for your convinience and readabilty of the code:

if Supports(FControlList[i], IMyControlEx, MyControlEx) then
begin
  MyControlEx.DoYourMagic(Self, SomeData);
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文