Shutdown memory leak reports using the console application

I created a console application and installed ReportMemoryLeaksOnShutdown: = True.

I created a TStringList but did not release it.

When the program completes, I see a memory leak for a short second, but then the console closes.

I tried to add ReadLn; to the end, but it only shows an empty console window when I do this, which makes sense.

I need to find a way to pause execution after reporting a memory leak, but until the program terminates.

I am 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. 
+6
source share
3 answers

The easiest way is to simply launch the application in a previously opened command window.

If you insist on seeing a memory leak report while working in the IDE, follow these steps:

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

Alternatively, as Sertac Akyuz commented, set a breakpoint at end. block system .

You can also redirect a 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 necessary parameters in FastMM4Options.inc

+6
source

This is a bug in recent versions of Delphi. I just tested it in this latest free Delphi 10.1 Starter, and it behaves the way you described, but since it does not provide RTL sources, I cannot verify the exact cause.

In Delphi XE2, it behaves as expected: it creates a dialog box with the specified model and expects a reaction from you, as described by Sertak.

In Delphi 10.1, a leak is actually reported to the console window, but the program does not stop to wait for the user's attention. This is a bad decision, both for this reason and for the possible use of console programs in scripts (CMD or PS scripts will not "understand" this message and may confuse it with a legitimate exit and fail to complete additional program steps.

I think you need to open a regression type error report over Delphi 10.0, but I don’t think they will fix it before release 10.2.

I also switched your application from the Delphi memory manager to the original one, and then the erroneous behavior was canceled: the program displayed a message box and waited until I let it go before leaving the IDE.

Currently, I suggest you use the mentioned original memory manager, and not its Delphi plug.

 program Project1; {$APPTYPE CONSOLE} uses FastMM4, System.Classes, System.SysUtils; ... 

The original memory manager is located at http://github.com/pleriche/FastMM4. You can use the Git client in your Delphi or standalone to update it, or you can download the code once and stop updating to you.

Relevant quotes of his code:

  {$ifdef LogErrorsToFile} {Set the message footer} LMsgPtr := AppendStringToBuffer(LeakMessageFooter, LMsgPtr, Length(LeakMessageFooter)); {Append the message to the memory errors file} AppendEventLog(@LLeakMessage[0], UIntPtr(LMsgPtr) - UIntPtr(@LLeakMessage[1])); {$else} {Set the message footer} AppendStringToBuffer(LeakMessageFooter, LMsgPtr, Length(LeakMessageFooter)); {$endif} {$ifdef UseOutputDebugString} OutputDebugStringA(LLeakMessage); {$endif} {$ifndef NoMessageBoxes} {Show the message} AppendStringToModuleName(LeakMessageTitle, LMessageTitleBuffer); ShowMessageBox(LLeakMessage, LMessageTitleBuffer); {$endif} end; end; {$endif} end; 

and

 {Shows a message box if the program is not showing one already.} procedure ShowMessageBox(AText, ACaption: PAnsiChar); begin if (not ShowingMessageBox) and (not SuppressMessageBoxes) then begin ShowingMessageBox := True; MessageBoxA(0, AText, ACaption, MB_OK or MB_ICONERROR or MB_TASKMODAL or MB_DEFAULT_DESKTOP_ONLY); ShowingMessageBox := False; end; end; 

This code depends on running on desktop Windows, so perhaps Embarcadero tried to β€œfix” it to make it cross-platform. However, as they did, it broke it on the Windows console ....

Also consider using other forms of logging β€” to a file and / or to Windows debugging lines. They will not be as attractive as a modal window, but at least they will help you save information if you know where to look for it.

+7
source
 var SaveExitProcessProc: procedure; s: TStringList; procedure MyExitProcessProc; begin ExitProcessProc := SaveExitProcessProc; readln; end; begin SaveExitProcessProc := ExitProcessProc; ExitProcessProc := MyExitProcessProc; ReportMemoryLeaksOnShutdown := True; s := TStringList.Create; end. 
+4
source

All Articles