Search code examples
delphimemory-leaksdelphi-10-seattle

Reporting memory leaks on shutdown with a console application


I've created a console application and set ReportMemoryLeaksOnShutdown := True.

I've created a TStringList but did not free it.

When the program finishes executing, I see the memory leak for a brief second but then the console closes.

I've tried adding a ReadLn; to the end, but it only shows a blank console window when I do that, which makes sense.

I need to find a way to pause executing after the memory leak report, but before complete program shutdown.

I'm using Delphi 10 Seattle.

program Project1;

{$APPTYPE CONSOLE}

uses
  System.Classes,
  System.SysUtils;

var
  s : TStringList;

begin
  try
    ReportMemoryLeaksOnShutdown := True;
    s := TStringList.Create;

    //ReadLn doesn't work here, which makes sense.
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  //I need to be able to pause the program somewhere after the end statement here.
end.

Solution

  • The easiest is to simply run the application in a previously opened command window.

    If you insist on seeing the memory leak report while running in the IDE, do as follows:

    • Locate the ShowMessage procedure in GetMem.inc (line 4856 in Delphi 10 Seattle)
    • Place a breakpoint on the end; of that procedure.

    Alternatively, as Sertac Akyuz commented, put a break point on the end. of the system unit.

    You can also redirect the memory leak report to a file. Download the full version of FastMM from

    https://sourceforge.net/projects/fastmm/

    or better, thanks to Arioch 'The, from here:

    https://github.com/pleriche/FastMM4

    and set the needed options in FastMM4Options.inc