【问题标题】:"ReportMemoryLeaksOnShutdown" not working in Delphi 10.2 Tokyo?“ReportMemoryLeaksOnShutdown”在 Delphi 10.2 Tokyo 中不起作用?
【发布时间】:2017-06-16 08:45:02
【问题描述】:

似乎设置ReportMemoryLeaksOnShutdown := true 对使用Delphi 10.2 Tokyo 创建的程序没有任何影响(我在Windows 和Linux 程序中尝试过)。即使有明显的内存泄漏,也不会报告任何内容。

有人可以确认吗?还有其他方法可以检查 Linux 程序中的内存泄漏吗?在 Windows 上,我可以使用 madExcept。

----------------- 编辑 2 ------------------

在 Delphi 10.2 ReportMemoryLeaksOnShutdown := true 中似乎只适用于未标记为控制台应用程序的程序。一旦我注释掉 {$APPTYPE CONSOLE} 行,我就会收到所需的错误消息(当我在 Windows 上运行程序时)。

----------------- 编辑 1 ------------------

这是请求的示例:

program WeakRefTest;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  SysUtils;

type
    TParent = class;

    TChild = class
      private
        {$IFDEF AUTOREFCOUNT} [Weak] {$ENDIF}
        Parent: TParent;
      public
        constructor Create (const Parent: TParent);
        destructor Destroy; override;
    end; { TChild }

    TParent = class
      private
        Child : TChild;
      public
        constructor Create;
        destructor Destroy; override;
    end; { TParent }

constructor TChild.Create(const Parent: TParent);
begin
    inherited Create;

    WriteLn ('TChild.Create');
    Self.Parent := Parent;
end;

destructor TChild.Destroy;
begin
    WriteLn ('TChild.Destroy');
    inherited;
end;

constructor TParent.Create;
begin
    inherited;

    WriteLn ('TParent.Create');
    Child := TChild.Create (Self);
end;

destructor TParent.Destroy;
begin
    WriteLn ('TParent.Destroy');
    inherited;
end;

procedure SubRoutine;

var
    Parent : TParent;

begin
    Parent := TParent.Create;
    WriteLn ('"SubRoutine" exit');
end; { SubRoutine }

begin { WeakRefTest }
    ReportMemoryLeaksOnShutdown := true;

    try
        SubRoutine;
        WriteLn ('"WeakRefTest" done');

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

为了在 Linux 上强制内存泄漏,注释掉 TChild 声明中带有 [Weak] 属性的行。为 Windows 编译时会出现内存泄漏,因为不支持 ARC。

当我使用 Delphi XE 编译和运行代码时,会出现一条消息,提示存在内存泄漏:

当我使用 Delphi 10.2 为 Windows 编译和运行时,没有任何显示。在TChild的声明中注释掉[Weak]属性后,使用Linux编译器时也是如此。

【问题讨论】:

  • 向 Embarcadero 提交错误报告
  • 我刚刚测试过(在 Windows FMX 项目上),它工作正常。可以举个例子吗?
  • 你应该用minimal reproducible example进行演示
  • 所以已经提交了错误报告。
  • @Craig Young:给你:var p : Pointer; begin ReportMemoryLeaksOnShutdown := true; GetMem(p, 100); end.

标签: delphi delphi-10.2-tokyo


【解决方案1】:

如果您从 cmd 窗口运行控制台应用程序,它将显示有关内存泄漏的相应消息。 内存泄漏报告的行为发生了变化,窗口应用程序显示 MessageBox,而控制台应用程序在控制台中获取消息。

在 Delphi XE2 中,ScanForMemoryLeaks 过程中有单个 MessageBoxA。 在 Delphi 10.2 中有自定义过程 ShowMessage(AMessage, ATitle: _PAnsiChr);它交替调用 WriteConsoleFile 或 MessageBoxA。 所以它是设计的,而不是错误(恕我直言)。

比较讨论:Reporting memory leaks on shutdown with a console application

【讨论】:

  • 感谢您澄清这一点。从我的角度来看,这还没有被正确考虑,但现在我知道是什么导致了这种行为,我总是可以使用FastMM4 或使用ExitProcessProc,如here 所述。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-14
相关资源
最近更新 更多