为什么 TList.Remove() 会产生 EAccessViolation 错误?
为什么执行下面的代码时会引发 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;
Why EAccessViolation is raised when executing the code below?
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它看起来像一个错误。
如果您使用 debug dcu 进行编译(通常不要这样做,除非您想失去理智!)您会发现对比较器的调用出错了。 比较函数的第三个值(可能是可选的)未设置并导致访问冲突。
因此,您可能无法将方法指针放入通用列表中。
好的,以下工作有效:
您必须定义自己的比较器,并可能具有更多智能;-)。
It looks like a bug.
If you compile with debug dcu's (normally don't do that unless you want to loose your sanity!) you see that a call to the comparer went wrong. A (possibly optional) third value of a compare function is not set and causes the access violation.
So possibly you can't put method pointers in a generic list.
Ok the following works:
You have to define your own comparer, with possiby some more intelligence ;-).
访问冲突是由于缺少比较器引起的。 我怀疑这已在补丁中修复,但如果您使用 TObjectList,问题仍然存在(至少在 Delphi 2009 中),所以我只是用最简单的解决方案进行更新:
或者就我而言
Access Violation is caused by missing comparer. I suspect this was fixed in a patch but the problem still persists (at least in Delphi 2009) if you use a TObjectList so I'm just updating with the simplest solution:
or in my case
是否可以将自定义比较器传递给
TList
? 我面前没有D2009,所以无法尝试。Is it possible to pass a custom comparer to
TList<T>
? I don't have D2009 in front of me, so can't try it.上面的代码在TForm1中使用...
the above code is used in TForm1 ...