Search code examples
delphidelphi-10.2-tokyo

"ReportMemoryLeaksOnShutdown" not working in Delphi 10.2 Tokyo?


It seems that setting ReportMemoryLeaksOnShutdown := true does not have any effect in programs created with Delphi 10.2 Tokyo (I tried it with Windows and Linux programs). Even if there are obvious memory leaks, nothing gets reported.

Can someone confirm this? And is there an alternative way to check for memory leaks in Linux programs? On Windows I could use madExcept.

------------------ Edit 2 ------------------

In Delphi 10.2 ReportMemoryLeaksOnShutdown := true only seems to work for programs that are not flagged as console apps. Once I comment out the line {$APPTYPE CONSOLE} I receive the desired error message (when I run the program on Windows).

------------------ Edit 1 ------------------

Here's the requested example:

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.

To force the memory leak on Linux comment out the line with the [Weak] attribute in the declaration of TChild. When compiling for Windows there will be memory leaks because ARC is not supported.

When I compile and run the code using Delphi XE a message appears that there are memory leaks: Message displayed when compiled with Delphi XE

When I compile and run for Windows using Delphi 10.2 nothing shows up. Same when using the Linux compiler after I commented out the [Weak] attribute in the declaration of TChild.


Solution

  • If you run the console application from cmd window it will show the appropriate message about the memory leak. The behavior of the memory leak report changed and the MessageBox is displayed for windowed applications, while console applications get the message in console.

    In Delphi XE2 there was single MessageBoxA in ScanForMemoryLeaks procedure. In Delphi 10.2 there is custom procedure ShowMessage(AMessage, ATitle: _PAnsiChr); which calls alternatively WriteConsoleFile or MessageBoxA. So it is designed, not the bug (IMHO).

    Compare the discussion: Reporting memory leaks on shutdown with a console application