Sorting TDictionary

I'm not experienced with Generic Collections. I need to sort a TDictionary.

type TSearchResult = TPair<Integer,double>;

var
   target_results : TDictionary<Integer, double>;
   session_search_results : array[0..max_searches] of TArray<TSearchResult>;

I'm sorting using this code

   session_search_results[session_search_id]:= target_results.ToArray;
   TArray.Sort<TSearchResult>(session_search_results[session_search_id],
                    TComparer<TSearchResult>.Construct(
                              function(const L, R: TSearchResult): Integer
                              begin
                                 Result := Round(R.Value - L.Value);
                              end
                    ));

Why do I get an Access Violation with this? What am I doing wrong?

Complement:

If I iterate through the array with

 for i:= 0 to Length(session_search_results[session_search_id])-1 do
      MyDebug(IntToStr(session_search_results[session_search_id][i].Key)+' = value = '
            + FloatToStr(session_search_results[session_search_id][i].Value));

I get an output of:

Debug Output: ==>CoreSearchText: array length=8<== Process TestApp.exe (2536)
Debug Output: ==>100007 = value = 19,515<== Process TestApp.exe (2536)
Debug Output: ==>100003 = value = 2,4<== Process TestApp.exe (2536)
Debug Output: ==>100005 = value = 12<== Process TestApp.exe (2536)
Debug Output: ==>100008 = value = 2,4<== Process TestApp.exe (2536)
Debug Output: ==>100002 = value = 2,4<== Process TestApp.exe (2536)
Debug Output: ==>100004 = value = 2,4<== Process TestApp.exe (2536)
Debug Output: ==>100009 = value = 40,515<== Process TestApp.exe (2536)
Debug Output: ==>100001 = value = 15<== Process TestApp.exe (2536)

When the sort is applied, the Access Violation crashes the application. The array seems to be ok. What can be the reason? Thanks!

Answers


This seems to be a codegen bug in XE (also existing in XE2) with a redeclared generic record and optimization turned on.

This program reproduces the bug:

program Project1;

{$APPTYPE CONSOLE}
{$O+}

uses
  Generics.Collections,
  Generics.Defaults,
  SysUtils;

type
  TSearchResult = TPair<Integer, Integer>;

function Compare(const L, R: TSearchResult): Integer;
begin
  Result := R.Value - L.Value;
end;

var
  values: TArray<TSearchResult>;
begin
  try
    SetLength(values, 3);
    TArray.Sort<TSearchResult>(values, TComparer<TSearchResult>.Construct(Compare));
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.

I have reported it as QC #106391.

A possible solution is to add the {$O-} to the unit that contains the call to TArray<T>.Sort.


Need Your Help

How do I debug Lift applications in Eclipse?

eclipse debugging scala lift

I come from a background in C++, Python, and Django and I'm trying to expand my horizons and learn Scala and Lift. However, I'm having a really hard time figuring out how to debug Lift applications...

Wordpress + jquery (inside functions.php) not working

javascript php jquery ajax wordpress

i was done adding this script into \mythemes\functions.php: