【问题标题】:Delphi Pascal XE4 Compiler bug?Delphi Pascal XE4 编译器错误?
【发布时间】:2013-09-12 13:14:09
【问题描述】:

我想知道我是否发现了一个 Embarcadero 编译器错误......

这个问题看起来和泛型有关。

这是我的源代码

unit u_DateCount;

interface

uses
  SysUtils,
  u_JavaScriptable
  ;

type
  TDateCount = class (TJavaScriptable)
    strict private
    public
      NoOfSamples : Integer;
      TheDate : TDate;
      function ToString():String; override;
  end;

implementation

function TDateCount.ToString():String;
var
    myYear, myMonth, myDay : Word;
begin
    DecodeDate(TheDate, myYear, myMonth, myDay);
    Result := Format('[new Date(%d, %d ,0), %d]', [myYear, myMonth, NoOfSamples]);
end;

end.

unit u_Javascriptable;

interface

type
  TJavaScriptable = class
    strict private
    public
      function ToString:String; override;
  end;

implementation

function TJavaScriptable.ToString:String;
begin
    Result := '';
end;

end.

unit u_LineChart;

interface

uses
  System.IOUtils,
  SysUtils,
  System.Generics.Collections,
  u_JavaScriptable
  ;

type
  TLineChart<RecordType : TJavaScriptable> = class
    strict private
      Template : String;
      function ConvertRecordsToString():String;
    public
      Records : TList<RecordType>;
      function ToString():String;
      constructor Create(templatePath : String);
      destructor Destroy(); override;
  end;

implementation

function TLineChart<RecordType>.ConvertRecordsToString():String;
var
    I: Integer;
begin
    //Open brackets
    Result := '[ ';

    //The first record
    if Records.Count > 0 then
    begin
        Result := Result + Records[0].ToString();
    end;

    //Loop over records
    for I := 1 to Records.Count - 1 do
    begin
        Result := Result + ', ' + Records[I].ToString();
    end;

    //Close bracket
    Result := Result + ' ]';
end;

function TLineChart<RecordType>.ToString():String;
begin
    Result := Format(Template, [ConvertRecordsToString()]);
end;

constructor TLineChart<RecordType>.Create(templatePath : String);
begin
    inherited Create();
    Template := TFile.ReadAllText(templatePath);
    Records := TList<RecordType>.Create();
end;

destructor TLineChart<RecordType>.Destroy();
var
    I: Integer;
begin
    if Assigned(Records) then
    begin
        for I := 0 to Records.Count - 1 do
        begin
            Records[I].Destroy();
        end;
        Records.Clear();
        Records.Destroy();
        Records := nil;
    end;

    inherited;
end;

end.

最后是主程序

program Project4;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  u_Javascriptable in 'u_Javascriptable.pas',
  u_LineChart in 'u_LineChart.pas',
  u_DateCount in 'u_DateCount.pas';

var
   lineChart : TLineChart<TDateCount>;

begin

  lineChart := TLineChart<TDateCount>.Create('linechart.html');
  try


  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

我尝试编译时收到的错误消息是

[dcc32 致命错误] Project4.dpr(30): F2084 内部错误: AV097530AC-R00000014-0

通常当我看到与此类似的错误消息时,我可以通过关闭 embarcadero IDE 并重新启动它来修复它。但是,这一次似乎不起作用。

【问题讨论】:

  • 我通常通过清理和重建我的项目来修复这个错误。
  • 谢谢,刚刚试过。它没有任何区别。
  • 尝试清理您的项目文件(与compilebuild 一起的另一个选项),这会删​​除所有已编译的文件,然后关闭/重新打开IDE,然后重建
  • 是的,试过了。我什至创建了一个重现错误的小项目。还是没有运气。
  • 我刚刚使用XE3成功编译了程序。这一定是 XE4 错误。

标签: delphi generics delphi-xe4


【解决方案1】:

问题在于TLineChart&lt;RecordType&gt;.Destroy()的实现。

Records[I].Destroy(); 更改为 Records[I].Free(); 即可。 或者你只是做正确并在构造函数中使用TObjectList&lt;RecordType&gt;.Create;,它负责在销毁列表时销毁其中的所有元素。

切勿直接致电Destroy。使用Free。虽然它不应该导致编译器错误,但无论如何它都是错误的。

【讨论】:

  • +1 虽然我会说在局部变量上使用 Destroy 很好,而不是在析构函数中。就个人而言,为了保持一致性,我总是使用 Free。
  • @DavidHeffernan 在这种情况下不相关,但 nextgen 编译器实际上将 Free 调用转换为 nil 赋值。如果您调用 Destroy,这与 nextgen 不兼容。
  • @StefanGlienke 是的,所有这些重大更改都与现有代码混淆。而且我不确定他们会带来新客户......
  • 在调用destroy之前免费检查nil。有趣的是,这种差异导致了编译时错误而不是运行时错误。
【解决方案2】:

如果编译器报告“内部错误”,则始终是编译器错误。您应该为此在 QC 中打开一张票。希望他们可以为 XE5 修复它。

【讨论】:

    【解决方案3】:

    由于这适用于 XE3 而不是 XE4,我将假定这是一个 XE4 错误。在解决此问题之前,解决方案是使用不同版本的编译器,例如 XE3。

    【讨论】:

    • 您可以编辑您的问题以添加重现该错误的小项目。然后将此项目提交到 QC 报告中。
    • 这是一个危险的结论。代码生成的微小变化会产生行为的微小变化,因此您没有足够的信息来得出该结论。对于另一段相关的代码,情况可能正好相反。
    猜你喜欢
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多