【问题标题】:Why is TList.Remove() producing an EAccessViolation error?为什么 TList.Remove() 会产生 EAccessViolation 错误?
【发布时间】:2008-11-14 11:22:03
【问题描述】:

为什么在执行下面的代码时会引发 EAccessViolation?

uses
  Generics.Collections;
  ...

var
  list: TList<TNotifyEvent>;
  ...

begin
  list := TList<TNotifyEvent>.Create();
  try
    list.Add(myNotifyEvent);
    list.Remove(myNotifyEvent);  // EAccessViolation at address...
  finally
    FreeAndNil(list);
  end;
end;

procedure myNotifyEvent(Sender: TObject);
begin
  OutputDebugString('event');  // nebo cokoliv jineho
end;

【问题讨论】:

    标签: delphi generics delphi-2009 tlist


    【解决方案1】:

    它看起来像一个错误。

    如果您使用调试 dcu 进行编译(通常不要这样做,除非您想失去理智!)您会看到对比较器的调用出错了。比较函数的第三个值(可能是可选的)未设置并导致访问冲突。

    因此,您可能无法将方法指针放在通用列表中。

    确定以下工作:

    uses
      Generics.Defaults;
    
    type
      TForm4 = class(TForm)
        ...
      private
        procedure myNotifyEvent(Sender: TObject);
      end;
    
    TComparer<T> = class (TInterfacedObject, IComparer<T>)
    public
      function Compare(const Left, Right: T): Integer;
    end;
    
    implementation
    
    uses
      Generics.Collections;
    
    var
      list: TList<TNotifyEvent>;
    begin
      list := TList<TNotifyEvent>.Create(TComparer<TNotifyEvent>.Create);
      try
        list.Add(myNotifyEvent);
        list.Remove(myNotifyEvent);
      finally
        FreeAndNil(list);
      end;
    end;
    
    procedure TForm4.myNotifyEvent(Sender: TObject);
    begin
      ShowMessage('event');
    end;
    
    { TComparer<T> }
    
    function TComparer<T>.Compare(const Left, Right: T): Integer;
    begin
      Result := 0;
    end;
    

    您必须定义自己的比较器,并可能具有更多智能;-)。

    【讨论】:

      【解决方案2】:

      访问冲突是由缺少比较器引起的。我怀疑这已在补丁中修复,但如果您使用 TObjectList,问题仍然存在(至少在 Delphi 2009 中),所以我只是用最简单的解决方案进行更新:

      TList<TNotifyEvent>.Create(TComparer<TNotifyEvent>.Default);
      

      或者在我的情况下

      TObjectList<TNotifyEvent>.Create(TComparer<TNotifyEvent>.Default);
      

      【讨论】:

      • xe4 仍然存在这个错误:/
      【解决方案3】:

      是否可以将自定义比较器传递给TList&lt;T&gt;?我面前没有D2009,所以不能尝试。

      【讨论】:

      • 是的,你可以在重载的构造函数中传递一个。
      【解决方案4】:

      以上代码在TForm1中使用...

      uses 
        Generics.Collections;
      
      procedure TForm1.Button1Click(Sender: TObject);
      var
        list: TList<TNotifyEvent>;
      begin
        list := TList<TNotifyEvent>.Create();
        try
          list.Add(myNotifyEvent);
          list.Remove(myNotifyEvent);  // EAccessViolation at address...
        finally
          FreeAndNil(list);
        end;
      end;
      procedure TForm1.myNotifyEvent(Sender: TObject);
      begin
        OutputDebugString('event');  // nebo cokoliv jineho
      end;
      

      【讨论】:

      • 嗯,这并不是您问题的真正答案。我认为你应该将它与你的问题中的代码合并(因为它现在是无效的,因为问题中显示的 myNotifyEventTNotifyEvent 签名不匹配(它不是那里的方法)。跨度>
      猜你喜欢
      • 2018-03-17
      • 1970-01-01
      • 1970-01-01
      • 2011-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-25
      相关资源
      最近更新 更多