来自 Delphi 支持的访问冲突异常 ->查询接口
我有以下代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
即使
FControlList[I]
不是nil
,也不意味着它指向有效数据。底层对象实例可能已经被释放。我还建议删除对
IMyControl
的类型转换。Supports
可以将对象和接口作为参数,即使它们是nil
,并产生所需的结果。此外,为了代码的方便性和可读性,您可以考虑使用这种调用
supports
的方式:Even if
FControlList[I]
is notnil
, 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 arenil
, and produce the desired result.In addition you may consider using this way of calling
supports
for your convinience and readabilty of the code: