Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.
like image 816
FLDelphi Avatar asked Sep 19 '16 18:09

FLDelphi


People also ask

How do I monitor memory leaks in Windows?

Check RAM With Windows' Memory Diagnostics Tool Press Windows key+R, enter "mdsched.exe," then select OK. Select Restart now and check for problems (recommended). The test will begin and may take several hours to complete. Follow any on-screen instructions once the test is complete.

How do you observe a memory leak?

To find a memory leak, you've got to look at the system's RAM usage. This can be accomplished in Windows by using the Resource Monitor. In Windows 11/10/8.1: Press Windows+R to open the Run dialog; enter "resmon" and click OK.

Does exit cause memory leaks?

Not under modern operating systems, no. The OS automatically collects all the memory when the process dies. In fact freeing memory can actually be detrimental for the performance if the program is exiting anyway.


Video Answer


1 Answers

This is certainly a hack, don't use in production :)

ReportMemoryLeaksOnShutdown:= True;
IsConsole:= False;
TStringList.Create;

However, it causes the leak message (and some other messages) to be displayed in a message box (where all text can be copied by pressing Ctrl+C).

(Tested with Delphi 10.2, please report any side effects we wouldn't like)

like image 118
maf-soft Avatar answered Oct 09 '22 11:10

maf-soft