德尔福TPair例外
发布时间:2020-12-15 04:18:14 所属栏目:大数据 来源:网络整理
导读:我有这个高峰来测试TPair.您可以在新的Delphi XE控制台应用程序上复制粘贴.我用例外标记了这一行: Project Project1.exe raised exception class EAccessViolation with message ‘Access violation at address 0045042D in module ‘Project1.exe’. Read
我有这个高峰来测试TPair.您可以在新的Delphi XE控制台应用程序上复制粘贴.我用例外标记了这一行:
任何的想法 ? 谢谢. program Project1; {$APPTYPE CONSOLE} uses SysUtils,Generics.Defaults,Generics.Collections; type TProduct = class private FName: string; procedure SetName(const Value: string); published public property Name: string read FName write SetName; end; type TListOfProducts = TObjectDictionary<TProduct,Integer>; { TProduct } procedure TProduct.SetName(const Value: string); begin FName := Value; end; var MyDict: TListOfProducts; MyProduct1: TProduct; MyProduct2: TProduct; MyProduct3: TProduct; APair: TPair<TProduct,Integer>; aKey: string; begin try MyDict := TListOfProducts.Create([doOwnsKeys]); MyProduct1 := TProduct.Create; MyProduct1.Name := 'P1'; MyProduct2 := TProduct.Create; MyProduct2.Name := 'P2'; MyProduct3 := TProduct.Create; MyProduct3.Name := 'P3'; MyDict.Add(MyProduct1,1); MyDict.Add(MyProduct2,2); MyDict.Add(MyProduct3,3); APair := MyDict.ExtractPair(MyProduct1); Writeln(APair.Key.Name); // <--- Error is Here. Writeln(IntToStr(APair.Value)); Readln(aKey); except on E: Exception do Writeln(E.ClassName,': ',E.Message); end; end. 解决方法
这是一个Delphi错误. TDictionary< TKey,TValue> .ExtractPair不分配结果.
RRUZ位于bug in QC. 代码如下: function TDictionary<TKey,TValue>.ExtractPair(const Key: TKey): TPair<TKey,TValue>; var hc,index: Integer; begin hc := Hash(Key); index := GetBucketIndex(Key,hc); if index < 0 then Exit(TPair<TKey,TValue>.Create(Key,Default(TValue))); DoRemove(Key,hc,cnExtracted); end; 在调用DoRemove时应该分配结果. 解决这个bug很难. ExtractPair是在不破坏密钥的情况下从字典中获取项目的唯一方法,因此您必须调用它.但由于它不会返回提取的项目,因此您需要先读取该项目,记住该值,然后调用ExtractPair. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |