我在程序中使用TObjectList类,在Remove一项时,有时出现Abstract Error,请教高手这是什么原因?

解决方案 »

  1.   

    abstract error出现的原因一般是因为你在基类中定义了纯虚方法,但是在子类中没有实现它,当它被使用时就会产生这样的error,
    但是在TObjectList中为什么会出现主要还要看你的代码怎么写的了,把代码贴出来看看。
      

  2.   

    我定义了一个类(简化版本)
      TMainModule = class
      private
        FModule : TObject;
      public
        constructor Create(_module: TObject);
        destructor Destory;
      end;constructor TMainModule.Create(_module: TObject);
    begin
      FModule := _module;
    end;destructor TMainModule.Destory;
    begin
      FModule.Free;
    end;用TMainModule类表示TObjectList中的一项。还定义了
      TMainModuleList = class
      protected
        List    : TObjectList;
        aModule : TMainModule;
      public
        constructor Create;
        destructor Destory;
        function RemoveModule(_module: TObject): Integer;
        function AddModule(_module: TObject): Integer;
      end;constructor TMainModuleList.Create;
    begin
      List := TObjectList.Create;
    end;destructor TMainModuleList.Destory;
    begin
      List.Free;
      aModule.Free;
    end;function TMainModuleList.AddModule(_module: TObject): Integer;
    begin
      aModule := TMainModule.Create(_module);
      Result := List.Add(aModule); 
    end;function TMainModuleList.RemoveModule(_module: TObject): TStatus;
    var
      i : Integer;
    begin
      i := IndexOf(_module);
      if i = -1 then Result := ERROR_MODULE_NOT_FOUND //_module do not exist
      else  //_module exist
      begin
        (List[i] as TMainModule).Modules.Free;
        List.Remove(List[i]);
        //如果在这之间调用List.Remove的话,并不能删除此项,但如果按上述代码执行的话
        //有时又会出现Abstract Error.
      end;
    end;用TMainModuleList管理一个TMainModule组成的List。谢谢!
      

  3.   

    (List[i] as TMainModule).Modules.Free;
    这句是不需要的,destructor Destory;
    这句有拼写错误,应该是:的destructor Destroy;
    并且要用override;//如果在这之间调用List.Remove的话,并不能删除此项,但如果按上述代码执行的话
    就是因为拼写错误,没有运行到你的destroy方法。
      

  4.   

    哎呀 不好意思 发错了在destructor TMainModule.Destory;
    begin
      FModule.Free;
    end;
    里面设断点看是否执行到了这里
      

  5.   

    现在我还有一个问题:当我从前面开始删起时,不会出现错误,但当我从最后一个开始Remove项时,就会出现"Access Error"之类的错误,Why?
      

  6.   

    按道理来说,list中的item一个一个删除的话,必须从后向前删,如:
    var
      List:TList;
    begin
      for i:=List.Count-1 downto 0 do begin
        (List.Items[I] as TObject).Free);
      end;
    ..但是你的问题到没有遇到过,还是把代码贴出来。