开发者

Access violation exception from Delphi's Supports -> QueryInterface

开发者 https://www.devze.com 2022-12-23 20:55 出处:网络
I have the following piece of code: for i := 0 to FControlList.Count - 1 do if Supports(IMyControl(FControlList[i]), IMyControlEx) then

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(FCon开发者_如何学JAVAtrolList[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!!!


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号