TObjectList< T&gt ;.在Delphi 2009中包含导致访问冲突的原因 [英] TObjectList<T>.Contains causes Access Violation in Delphi 2009

查看:55
本文介绍了TObjectList< T&gt ;.在Delphi 2009中包含导致访问冲突的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Delphi 2009中,到目前为止,我对泛型没有大问题(使用Generics.Collections列表,没有特殊的泛型功能)。

In Delphi 2009, I had no major problems with Generics so far (using Generics.Collections lists, with no special Generics features).

现在我找到了这段代码会在访问 MyList的行中导致AV。包含

Now I found this code will cause an AV in the line which accesses MyList.Contains.

如果我声明TMyList = class(TList< TMyEntry>);

The error disappears if I declare TMyList = class(TList<TMyEntry>);

我应该避免 TObjectList< T> 还是我的代码中其他原因导致此错误?

Should I avoid TObjectList<T> or is something else in my code causing this error?

type
  TMyEntry = class(TStringlist);
  TMyList = class(TObjectList<TMyEntry>);

procedure TListTests.TestAV;
var
  Entry: TMyEntry;
  MyList: TMyList;
begin
  MyList := TMyList.Create;
  try
    Entry := TMyEntry.Create;

    MyList.Add(Entry);

    Assert(MyList.Contains(Entry));  // <--- AV

  finally
    MyList.Free;
  end;
end;


推荐答案

这是基于链接的答案

更改

MyList := TMyList.Create;

MyList := TMyList.Create(TComparer<TMyEntry>.Default);

这篇关于TObjectList&lt; T&gt ;.在Delphi 2009中包含导致访问冲突的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆