Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.


Welcome, Guest
Guest Settings
Help

Thread: "ReportMemoryLeaksOnShutdown" not working in Delphi 10.2 Tokyo?



Permlink Replies: 5 - Last Post: Jun 16, 2017 11:42 AM Last Post By: Olaf Hess
Olaf Hess

Posts: 11
Registered: 10/10/08
"ReportMemoryLeaksOnShutdown" not working in Delphi 10.2 Tokyo?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 16, 2017 2:24 AM
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.

Here's the example code I used on Linux. To force the memory leak comment out the line with the [Weak] attribute in the declaration of TChild:
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.


Edited by: Olaf Hess on Jun 16, 2017 5:44 AM
Lajos Juhasz

Posts: 801
Registered: 3/14/14
Re: "ReportMemoryLeaksOnShutdown" not working in Delphi 10.2 Tokyo?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 16, 2017 2:40 AM   in response to: Olaf Hess in response to: Olaf Hess
Olaf Hess wrote:

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.

I've tested with the following code:

procedure TForm1.Button1Click(Sender: TObject);
var x: TStringList;
begin
  x:=TStringList.Create;
end;


I hit 10 times the button and on the shut down as expected I got the
report (Tested on win32 and win64 targets. I don't have Linux to test.).

---------------------------
Unexpected Memory Leak
---------------------------
An unexpected memory leak has occurred. The unexpected small block
leaks are:  13 - 20 bytes: Unknown x 10 69 - 76 bytes: TStringList x 10
---------------------------
OK   
---------------------------
Olaf Hess

Posts: 11
Registered: 10/10/08
Re: "ReportMemoryLeaksOnShutdown" not working in Delphi 10.2 Tokyo?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 16, 2017 5:50 AM   in response to: Lajos Juhasz in response to: Lajos Juhasz
Thanks for your answer. I checked on two machines and I do not see any notifications about memory leaks when using 10.2 (both Windows and Linux programs). Strange ...

Lajos Juhasz wrote:
I've tested with the following code:

procedure TForm1.Button1Click(Sender: TObject);
var x: TStringList;
begin
  x:=TStringList.Create;
end;


I hit 10 times the button and on the shut down as expected I got the
report (Tested on win32 and win64 targets. I don't have Linux to test.).

Olaf Hess

Posts: 11
Registered: 10/10/08
Re: "ReportMemoryLeaksOnShutdown" not working in Delphi 10.2 Tokyo?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 16, 2017 6:09 AM   in response to: Lajos Juhasz in response to: Lajos Juhasz
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). I'll file a bug report.

Lajos Juhasz wrote:

I've tested with the following code:

procedure TForm1.Button1Click(Sender: TObject);
var x: TStringList;
begin
  x:=TStringList.Create;
end;


I hit 10 times the button and on the shut down as expected I got the
report (Tested on win32 and win64 targets. I don't have Linux to test.).

---------------------------
Unexpected Memory Leak
---------------------------
An unexpected memory leak has occurred. The unexpected small block
leaks are:  13 - 20 bytes: Unknown x 10 69 - 76 bytes: TStringList x 10
---------------------------
OK   
---------------------------
Dave Nottage

Posts: 1,850
Registered: 1/7/00
Re: "ReportMemoryLeaksOnShutdown" not working in Delphi 10.2 Tokyo? [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 16, 2017 6:34 AM   in response to: Olaf Hess in response to: Olaf Hess
Olaf Hess wrote:

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).

The message appears in a window only if it is a GUI app. If it's a console app, the message appears in the console
window. Run the project from the command line and you'll see.

--
Dave Nottage [MVP, TeamB]
Find hints, tips and tricks at Delphi Worlds blog: http://www.delphiworlds.com
Olaf Hess

Posts: 11
Registered: 10/10/08
Re: "ReportMemoryLeaksOnShutdown" not working in Delphi 10.2 Tokyo? [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 16, 2017 11:41 AM   in response to: Dave Nottage in response to: Dave Nottage
Thanks for pointing this out. I found an explanation -- and workarounds -- for this changed behavior here:
https://stackoverflow.com/questions/39579740/reporting-memory-leaks-on-shutdown-with-a-console-application

Dave Nottage wrote:
Olaf Hess wrote:

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).

The message appears in a window only if it is a GUI app. If it's a console app, the message appears in the console
window. Run the project from the command line and you'll see.

--
Dave Nottage [MVP, TeamB]
Find hints, tips and tricks at Delphi Worlds blog: http://www.delphiworlds.com
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02