- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这个尖峰来测试 TPair。您可以在新的 Delphi XE Console 应用程序上复制+粘贴。我已经标记了异常(exception)的行:
Project Project1.exe raised exception class EAccessViolation with message 'Access violation at address 0045042D in module 'Project1.exe'. Read of address A9032D0C.
有什么想法吗?
谢谢。
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
不分配 Result
.
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;
Result
当调用 DoRemove
时应分配已制作完成。
解决这个错误非常困难。 ExtractPair
是在不破坏 key 的情况下从字典中获取项目的唯一方法,因此您必须调用它。但由于它不会返回提取的项目,因此您需要首先读取该项目,记住该值,然后调用 ExtractPair
.
关于Delphi TPair 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5861857/
我有这个尖峰来测试 TPair。您可以在新的 Delphi XE Console 应用程序上复制+粘贴。我已经标记了异常(exception)的行: Project Project1.exe ra
uses Generics.Collections, types, rtti, System.Generics.Collections; procedure GimmePairArray(cons
我写了代码 procedure Pair; var PairList: TList>; LPair: TPair; begin PairList := TList>.Create; t
我在 delphi xe5 update 2 中编译了以下代码示例。 procedure TForm1.FormCreate(Sender: TObject); var i,t:Integer;
在 32 位和 64 位下编译时,TPair 的默认排序似乎存在差异。在 32 位下,默认排序的行为就像按对的键排序,在 64 位下,它看起来按值排序。这是可以预料的,还是我错过了什么? 使用 Del
我是一名优秀的程序员,十分优秀!