在将TDictionary排序成数组后正确处理TDictionary的方法

Mig*_*l E 5 delphi memory-management tdictionary delphi-xe

我有像TDictionary这样的

target_results : TDictionary<longint,double>;
Run Code Online (Sandbox Code Playgroud)

填充后我需要对结果进行排序.我是这样做的

type
  TSearchResult = TPair<longint,double>;

var
 target_results_array : TArray<TSearchResult>;

target_results_array:= target_results.ToArray;
TArray.Sort<TSearchResult>(best_knowledge_search_results,
                    TComparer<TSearchResult>.Construct(
                              function(const L, R: TSearchResult): Integer
                              begin
                                if L.Value < R.Value then Result := 1 else if L.Value > R.Value then Result := -1 else Result := 0;
                              end
                    ));
Run Code Online (Sandbox Code Playgroud)

这一切都按预期工作.我的问题是如何在没有任何泄漏的情况下处理TDictionary和TArray?目前我正在做

target_results.Free;
Run Code Online (Sandbox Code Playgroud)

Mas*_*ler 6

由于您的数据是longint和double,两者都是值类型,因此释放字典将正常工作.您的数组将包含原始值的副本,您无需担心丢失或损坏任何内容.

如果您有对象类型,并且您TObjectDictionary拥有对象,那么您必须担心这类事情,但对于纯粹的数字数据,您没问题.