问题描述
我已经看到很多Delphi 2009泛型中提到的错误,但从来没有预料到这样的基本的错误在Update 3中失败。如果列表包含1个或多个项目,则在通用TList或TObjectList上调用IndexOf将导致访问冲突: 键入
TTest = class(TObject);
程序DoTest;
var
list:TObjectList< TTest> ;;
t:TTest;
begin
list:= TObjectList< TTest>。创建;
try
t:= TTest.Create;
list.IndexOf(t); //列表中没有项目,正确的结果-1
list.Add(t);
list.IndexOf(t); //访问冲突
finally
list.Free;
结束
结束
异常是EAccessViolation:访问冲突在地址0048974C在模块'testbed.exe'。地址00000000
使用调试DCU进行编译会导致generics.collections.pas中的问题 - FComparer成员未分配:
function TList< T> .IndexOf(const Value:T):Integer;
var
i:整数;
begin
for i:= 0 to Count - 1 do
if FComparer.Compare(FItems [i],Value)= 0 then
Exit(i);
结果:= -1;
结束
这当然使得通用的TList几乎完全没用。由于更新3似乎没有修复这个错误,除了升级到XE之外,我有其他的追索权?
有一个看这个问题。
特别是,尝试创建您的TList像这样
TList< TTest> .Create(TComparer< TTest> .Default);
I've seen many mentions of bugs in Delphi 2009 generics, but never expected something so basic to fail in Update 3, no less. Calling IndexOf on a generic TList or TObjectList causes an access violation if the list contains 1 or more items:
type
TTest = class( TObject );
procedure DoTest;
var
list : TObjectList< TTest >;
t : TTest;
begin
list := TObjectList< TTest >.Create;
try
t := TTest.Create;
list.IndexOf( t ); // No items in list, correct result -1
list.Add( t );
list.IndexOf( t ); // Access violation here
finally
list.Free;
end;
end;
The exception is "EAccessViolation: Access violation at address 0048974C in module 'testbed.exe'. Read of address 00000000"
Compiling with debug DCUs leads to a problem in generics.collections.pas - the FComparer member is not assigned:
function TList<T>.IndexOf(const Value: T): Integer;
var
i: Integer;
begin
for i := 0 to Count - 1 do
if FComparer.Compare(FItems[i], Value) = 0 then
Exit(i);
Result := -1;
end;
This of course makes the generic TList almost completely useless. Since Update 3 does not seem to have fixed this bug, do I have a recourse other than upgrading to XE?
Have a look at this question. Generics problem
In particular, try creating your TList like this
TList<TTest>.Create(TComparer<TTest>.Default);
这篇关于通用TList在Delphi 2009中,IndexOf崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!