"ReportMemoryLeaksOnShutdown" 不在 Delphi 10.2 东京工作?

"ReportMemoryLeaksOnShutdown" not working in Delphi 10.2 Tokyo?

似乎设置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 编译器时相同

如果您 运行 来自 cmd window 的控制台应用程序,它将显示有关内存泄漏的适当消息。 内存泄漏报告的行为已更改,windowed 应用程序显示 MessageBox,而控制台应用程序在控制台中获取消息。

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

比较讨论: