【发布时间】:2012-07-12 21:57:17
【问题描述】:
我在 Delphi 2010 上开始项目,然后迁移到 XE,现在我尝试迁移到 XE2。 在 XE2(更新 4 修补程序 1)中编译后,单元测试开始以 AV 失败。 经过一番调试,很明显以下代码没有正确编译:
program ForwardDeclaration;
{$APPTYPE CONSOLE}
uses
System.SysUtils;
type
TEntityBase = class(TObject)
protected
FModel: Integer;
public
constructor Create(const AModel: Integer);
end;
TEntity<TKey> = class(TEntityBase)
end;
TMyEntity2 = class;
TMyEntity1 = class(TEntity<Integer>)
FData: Integer;
end;
TMyEntity2 = class(TMyEntity1)
end;
constructor TEntityBase.Create(const AModel: Integer);
begin
inherited Create;
FModel := AModel;
end;
var
MyEntity: TMyEntity1;
begin
try
Writeln(TEntityBase.ClassName, ': ', TEntityBase.InstanceSize, ' bytes');
Writeln(TMyEntity1.ClassName, ': ', TMyEntity1.InstanceSize, ' bytes');
MyEntity := TMyEntity1.Create(100);
Assert(MyEntity.FData = 0);
except
on E: Exception do Writeln(E.ClassName, ': ', E.Message);
end;
end.
程序输出:
TEntityBase: 12 bytes
TMyEntity1: 12 bytes <-- Must be 16 bytes!
EAssertionFailed: Assertion failure (ForwardDeclaration.dpr, line 41)
是否可以通过调整编译器选项来解决问题?
这个问题是否会在其他人身上重复出现?
附: QC107110
【问题讨论】:
-
将此提交给 QualityCentral。此外,前向声明在哪里进入。唯一没有使用这样的声明。
-
代码是最简化的,所以编译器错误被重现了。在实际项目中使用前向声明。如果这个问题不仅在我身上,那么我会写信给 QC。
-
好的。当您提交错误时,您应该进一步删除它。看起来前向声明不相关。只是 InstanceSize 的代码生成问题。如果您有支持合同,您可以提出支持问题,尽管这样做并不一定意味着您会得到回应。
-
没有前向声明代码效果很好,并且 TMyEntity1.InstanceSize 是 16 字节。没有泛型代码运行良好。问题在于前向声明和泛型。
-
这很有趣。未使用的前向声明的存在会改变行为。诡异的! FWIW,您的代码按预期运行,
TMyEntity1.InstanceSize=16在 XE2 更新 3 上。
标签: delphi delphi-xe2