CRT 不打印内存泄漏的行号
CRT Doesn't print line number of memory leak
我有下面的代码,我认为,基于Finding Memory Leaks Using the CRT Library,应该打印出内存泄漏的行号。
#include "stdafx.h"
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include <iostream>
void derp()
{
int* q = new int;
}
int main()
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
derp();
return 0;
}
当我 运行 它时,我得到以下信息:
Detected memory leaks!
Dumping objects ->
{75} normal block at 0x0067E930, 4 bytes long.
Data: < > CD CD CD CD
Object dump complete.
根据 Microsoft 的文档,我希望看到分配泄漏内存的行的打印输出,但我没有。
我做错了什么?我正在使用 VS2015。
应该已经在某处回答了。
"These techniques work for memory allocated using the standard CRT
malloc function. If your program allocates memory using the C++ new
operator, however, you need to redefine new if you want to see the
file and line numbers in the memory-leak report."
结果您将拥有新的运算符定义行。你可以使用 new 可以接受额外参数的技巧,其中一些可以默认为宏定义的值,例如 __LINE__
和 __FILE__
来自MSDN topic:
These techniques work for memory allocated using the standard CRT
malloc function. If your program allocates memory using the C++ new
operator, however, you may only see the file and line number where the
implementation of global operator new calls _malloc_dbg in the
memory-leak report. Because that behavior is not very useful, you can
change it to report the line that made the allocation by using a macro
that looks like this:
#ifdef _DEBUG
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
// Replace _NORMAL_BLOCK with _CLIENT_BLOCK if you want the
// allocations to be of _CLIENT_BLOCK type
#else
#define DBG_NEW new
#endif
然后将代码中的 new
替换为 DBG_NEW
。我测试了它,它可以与您的代码一起正常工作。
实际上,将代码中所有地方的 new
替换为 DBG_NEW
太繁琐了,所以您可以使用这个宏:
#ifdef _DEBUG
#define new new( _NORMAL_BLOCK , __FILE__ , __LINE__ )
#else
#define new new
#endif
我测试了这个方法,它也有效。
查看答案 here。您想将重载的 new
运算符与解决方案中指定的附加参数一起使用,以获取该信息。
在这种情况下,请更改线路
int* q = new int;
到
int* q = new (_NORMAL_BLOCK, __FILE__, __LINE__) int;
你应该会看到泄漏。
我有下面的代码,我认为,基于Finding Memory Leaks Using the CRT Library,应该打印出内存泄漏的行号。
#include "stdafx.h"
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include <iostream>
void derp()
{
int* q = new int;
}
int main()
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
derp();
return 0;
}
当我 运行 它时,我得到以下信息:
Detected memory leaks!
Dumping objects ->
{75} normal block at 0x0067E930, 4 bytes long.
Data: < > CD CD CD CD
Object dump complete.
根据 Microsoft 的文档,我希望看到分配泄漏内存的行的打印输出,但我没有。
我做错了什么?我正在使用 VS2015。
应该已经在某处回答了。
"These techniques work for memory allocated using the standard CRT malloc function. If your program allocates memory using the C++ new operator, however, you need to redefine new if you want to see the file and line numbers in the memory-leak report."
结果您将拥有新的运算符定义行。你可以使用 new 可以接受额外参数的技巧,其中一些可以默认为宏定义的值,例如 __LINE__
和 __FILE__
来自MSDN topic:
These techniques work for memory allocated using the standard CRT malloc function. If your program allocates memory using the C++ new operator, however, you may only see the file and line number where the implementation of global operator new calls _malloc_dbg in the memory-leak report. Because that behavior is not very useful, you can change it to report the line that made the allocation by using a macro that looks like this:
#ifdef _DEBUG
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
// Replace _NORMAL_BLOCK with _CLIENT_BLOCK if you want the
// allocations to be of _CLIENT_BLOCK type
#else
#define DBG_NEW new
#endif
然后将代码中的 new
替换为 DBG_NEW
。我测试了它,它可以与您的代码一起正常工作。
实际上,将代码中所有地方的 new
替换为 DBG_NEW
太繁琐了,所以您可以使用这个宏:
#ifdef _DEBUG
#define new new( _NORMAL_BLOCK , __FILE__ , __LINE__ )
#else
#define new new
#endif
我测试了这个方法,它也有效。
查看答案 here。您想将重载的 new
运算符与解决方案中指定的附加参数一起使用,以获取该信息。
在这种情况下,请更改线路
int* q = new int;
到
int* q = new (_NORMAL_BLOCK, __FILE__, __LINE__) int;
你应该会看到泄漏。