天天看点

VC++/Qt Creator内存泄漏检测方法(1):_CrtSetDbgFlag与_CrtDumpMemoryLeaks

微软官网说明

finding-memory-leaks-using-the-crt-library

https://docs.microsoft.com/en-us/visualstudio/debugger/finding-memory-leaks-using-the-crt-library?view=vs-2019 VS使用MFC开发项目的话,有自带的简单内存泄漏提示。例如:

Detected memory leaks!
Dumping objects ->
{1474} normal block at 0x0000000000335DF0, 16 bytes long.
 Data: <  4             > 10 80 34 00 00 00 00 00 00 00 00 00 00 00 00 00 
{1473} normal block at 0x0000000000348010, 960 bytes long.
 Data: <                > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
{1472} normal block at 0x0000000000335DA0, 16 bytes long.
 Data: <`               > 60 D4 87 03 00 00 00 00 00 00 00 00 00 00 00 00 
{1471} normal block at 0x000000000387D460, 3200 bytes long.
 Data: <                > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
{1470} normal block at 0x0000000003879040, 712 bytes long.
 Data: <  ,@      ,@    > 80 94 2C 40 01 00 00 00 D8 94 2C 40 01 00 00 00 
Object dump complete.      

使用crtdbg方法检测内存泄漏

那Win32或者Qt Creator的程序呢?Visual Studio调试器和C运行时库(CRT)可以帮助您检测和识别内存泄漏。需要自行添加代码检测。解决方法如下:

0、cpp源文件需要添加以下内容

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>      

1、在main()程序开始的地方,加上代码:

_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); //A

_CrtSetBreakAlloc(1472); //1472为上面内存泄漏的块号。

_CrtSetBreakAlloc(1471);

_CrtSetBreakAlloc(1470);

或者在结束的地方,加上:

_CrtDumpMemoryLeaks(); //B

A和B语句二选一。如果应用程序只有1个出口,请在应用程序退出点之前调用_CrtDumpMemoryLeaks,退出时会显示内存泄漏报告。如果您的应用有多个出口,则无需_CrtDumpMemoryLeaks在每个出口处手动放置,请在应用程序的开头放_CrtSetDbgFlag。

2、用Debug模式运行,程序自动断点在“内存块1472”分配的位置。

/* break into debugger at specific memory allocation */
if (_crtBreakAlloc != -1L && lRequest == _crtBreakAlloc)
_CrtDbgBreak();      

3、我们已经找到了泄漏的那块内存分配的底层操作的地方了。这个时候一直利用“调试”-“退出”,或快捷键“SHIFT+F11”跳转,直到看到了自己的代码。

4、检查自己在该代码中分配的内存块,是否进行了合理的释放。

5、问题解决之后,记得把_CrtSetBreakAlloc(xxx); 这句话删除掉,否则每次debug都断点,耽误事儿。

6、注意到了 _CrtSetDbgFlag(...) 这个函数没?它的说明如下:

Retrieves or modifies the state of the _crtDbgFlag flag to control the allocation behavior of the debug heap manager (debug version only).

_CRTDBG_LEAK_CHECK_DF 说明如下:

Perform automatic leak checking at program exit through a call to _CrtDumpMemoryLeaks and generate an error report if the application failed to free all the memory it allocated.

Qt Creator应用案例

LiteIDE是使用Qt写的应用程序,里面大量应用了crtdbg内存泄露检测的方法

https://github.com/visualfc/liteide

main.cpp入口处有:

#if defined(_MSC_VER) && defined(_DEBUG)

   _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);

#endif

每个cpp文件都有以下宏定义:

//lite_memory_check_begin
#if defined(WIN32) && defined(_MSC_VER) &&  defined(_DEBUG)
     #define _CRTDBG_MAP_ALLOC
     #include <stdlib.h>
     #include <crtdbg.h>
     #define DEBUG_NEW new( _NORMAL_BLOCK, __FILE__, __LINE__ )
     #define new DEBUG_NEW
#endif
//lite_memory_check_end      

个人建议把宏定义放在预编译头文件,这样每个cpp文件都可以检测内存泄露。

姊妹篇

另外,内存泄漏检测除了本篇提到的方法之外,还可以使用Visual Leak Detector或者Windbg。详情见:

VC++内存泄漏检测方法(2):Checkpoint/DumpStatistics VC++内存泄漏检测方法(3):Visual Leak Detector(VLD内存泄漏检测工具) VC++内存泄漏检测方法(4):【第1篇的补充说明】详解基于Crt的内存泄漏检测 VC++内存泄漏检测方法(5):使用强大的Windbg工具

继续阅读