0

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

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

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

在 Delphi 10.2ReportMemoryLeaksOnShutdown := 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 上强制内存泄漏,请注释掉[Weak]声明中带有属性的行TChild。为 Windows 编译时会出现内存泄漏,因为不支持 ARC。

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

当我使用 Delphi 10.2 为 Windows 编译和运行时,什么也没有出现。[Weak]在我注释掉TChild.

4

1 回答 1

6

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

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

比较讨论:使用控制台应用程序报告关机时的内存泄漏

于 2017-06-16T15:01:37.097 回答