我没有使用泛型集合的经验。我需要对一个TDictionary进行排序。
type TSearchResult = TPair<Integer,double>;
var
target_results : TDictionary<Integer, double>;
session_search_results : array[0..max_searches] of TArray<TSearchResult>;我正在使用以下代码进行排序
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
));为什么我会遇到访问冲突?我做错了什么?
补充:
如果我在数组中使用
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));我得到的输出是:
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)当应用排序时,访问冲突会使应用程序崩溃。数组看起来没问题。可能的原因是什么?谢谢!
发布于 2012-06-14 04:11:53
这似乎是XE中的一个codegen bug (也存在于XE2中),并启用了重新声明的泛型记录和优化。
这个程序会重现这个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.我已经将其报告为QC #106391。
一种可能的解决方案是将{$O-}添加到包含对TArray.Sort的调用的单元。
https://stackoverflow.com/questions/11012856
复制相似问题