Generic TList <> in Delphi 2009 crash on IndexOf

I saw a lot of mentions of bugs in Delphi 2009 generators, but I never expected something so bad in update 3, no less. An IndexOf call for a generic TList or TObjectList causes an access violation if the list contains 1 or more elements:

 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 the testbed.exe module." Reading address 00000000 "

Compiling with debug DCUs results in a problem in generics.collections.pas - 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 a common TList almost completely useless. Since Update 3 does not seem to have fixed this error, do I have a regression besides upgrading to XE?

+3
source share
2 answers

Take a look at this question. Why is TList.Remove () raising an EAccessViolation error?

In particular, try creating your TList as follows

 TList<TTest>.Create(TComparer<TTest>.Default); 
+8
source

This is a bug in the default constructor of TObjectList<T> , and I thought it was fixed in update 3. If you still see this, use a different constructor or just upgrade to D2010 or XE, where it is definitely fixed, (And you really want to exit D2009 if you want to work with generics).

+5
source

Source: https://habr.com/ru/post/974047/


All Articles