C++ 为什么输出内存泄露报告时不显示文件名和行号
#define _CRTDBG_MAP_ALLOC
#include
<
stdlib.h>
#include
<
crtdbg.h>
我想在一个控制台程序中输出内存泄露的详细信息,包括文件名和行号,但是添加了以上代码还是没有输出文件名,输出如下:
Detected memory leaks!
Dumping objects ->
{186338} normal block at 0x04915978, 40 bytes long.
Data: < P P P > B8 8C 50 00 B8 8C 50 00 B8 8C 50 00 CD CD CD CD
{186337} normal block at 0x049265F0, 48 bytes long.
Data: < > 1F 00 00 00 BA CD B8 F6 B5 E7 BB B0 20 00 FE FE
{186336} normal block at 0x04926580, 48 bytes long.
Data: < ddddddd > 02 00 00 00 64 64 64 64 64 64 64 00 FE FE FE FE
{186335} normal block at 0x04926510, 48 bytes long.
Data: < 13213 > 03 00 00 00 31 33 32 31 33 00 FE FE FE FE FE FE
{186334} normal block at 0x049264A0, 48 bytes long.
Data: < _ > 06 00 00 00 5F 00 FE FE FE FE FE FE FE FE FE FE
{186333} normal block at 0x04926430, 48 bytes long.
使用的软件是VS2008,在类的祈构函数中调用_CrtDumpMemoryLeaks()。我试着删除、添加、修改#define _CRTDBG_MAP_ALLOC代码顺序时发现输出都是这样,求高手解惑~
╓八千◆流╮
12 years, 1 month ago
Answers
这里需要一点用到宏定义的小技巧。
setDebugNew.h文件
#ifndef SET_DEBUG_NEW_H
#define SET_DEBUG_NEW_H
#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_CLIENTBLOCK
#endif
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif
#endif // end SET_DEBUG_NEW_H
memoryLeakDetect.cpp
#include "setDebugNew.h"
int main(int argc, char *argv[])
{
#ifdef _DEBUG
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
int *i = new int;
return 0;
}
运行结果输出如下:
Detected memory leaks!
Dumping objects ->
h:\program\windows\memoryleakdetect\memoryleakdetect\memoryleakdetect.cpp(9) : {67} client block at 0x00604A00, subtype 0, 4 bytes long.
Data: < > CD CD CD CD
Object dump complete.
程序“[9544] memoryLeakDetect.exe: 本机”已退出,返回值为 0 (0x0)。
以前在学习QT的时候简单了解过内存溢出检测这块,可以参考下我之前写的一个总结:
Qt在VS中内存溢出检测
Kagura
answered 12 years, 1 month ago