使用控制台应用程序关闭时报告内存泄漏 [英] Reporting memory leaks on shutdown with a console application

查看:117
本文介绍了使用控制台应用程序关闭时报告内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个控制台应用程序,并设置了ReportMemoryLeaksOnShutdown:= True.

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

我创建了一个TStringList,但是没有释放它.

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.

我尝试添加一个ReadLn;到最后,但是当我这样做时,它仅显示一个空白的控制台窗口,这是有道理的.

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.

我正在使用Delphi 10 Seattle.

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.

推荐答案

最简单的方法是简单地在先前打开的命令窗口中运行应用程序.

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

如果您坚持在IDE中运行时看到内存泄漏报告,请执行以下操作:

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

  • 在GetMem.inc中找到ShowMessage过程(Delphi 10 Seattle中的4856行)
  • 在该过程的end;上放置一个断点.
  • Locate the ShowMessage procedure in GetMem.inc (line 4856 in Delphi 10 Seattle)
  • Place a breakpoint on the end; of that procedure.

或者,正如Sertac Akyuz所说,在system单元的end.上设置一个断点.

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

您还可以将内存泄漏报告重定向到文件.从以下位置下载完整版本的FastMM:

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

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

或者更好,这要感谢来自这里的Arioch'The:

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

https://github.com/pleriche/FastMM4

并在FastMM4Options.inc

这篇关于使用控制台应用程序关闭时报告内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆