[英]Visual Studio memory leak detection not printing file name and line number
我想检查我的程序是否存在内存泄漏并找到了这篇 Microsoft 文章。
我彻底遵循了这篇文章并添加了
#define CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
和
_CrtDumpMemoryLeaks();
当程序退出时。
它在我的输出窗口中正确转储了所有内存泄漏信息,但问题是:
它不会打印内存泄漏所在的文件名和行号!
它在文章中说,使用#define _CRTDBG_MAP_ALLOC
它会打印文件名和行号,但它不适合我。
我的输出看起来像这样
Detected memory leaks!
Dumping objects ->
{3456} normal block at 0x038F81E8, 560 bytes long.
Data: < A B> 00 00 00 00 00 00 10 41 00 00 00 FF 00 00 E6 42
{3447} normal block at 0x038F8170, 56 bytes long.
Data: < B ^ B > 80 42 90 03 10 02 5E 08 80 42 90 03 00 00 CD CD
{3440} normal block at 0x038F86B0, 840 bytes long.
Data: < A B> 00 00 00 00 00 00 10 41 00 00 00 FF 00 00 A8 42
...
所以我真的无法解决这个问题......也按 F4 转到该行不起作用。
请你帮助我好吗?
你#define
是错误的。 为了得到格式
Detected memory leaks!
Dumping objects ->
C:\PROGRAM FILES\VISUAL STUDIO\MyProjects\leaktest\leaktest.cpp(20) : {18}
normal block at 0x00780E80, 64 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.
您需要使用:
#define _DEBUG
#define _CRTDBG_MAP_ALLOC
您还必须包含_DEBUG
,因为_CRTDBG_MAP_ALLOC
仅适用于_DEBUG
定义(源)。 同样从这个答案中确保#define
位于您要检查的 cpp 文件中。
我希望这会有所帮助,如果你还没有弄清楚 @AD,适用于 win32 应用程序,我们需要覆盖新的运算符。 不幸的是,它不适用于 MFC 应用程序。:(
#define _CRTDBG_MAP_ALLOC
#include<iostream>
#include <crtdbg.h>
#ifdef _DEBUG
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif
int main()
{
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ) ;
char *a = new char[10];
return 0;
}
顶部的 VS2013 文档示例中似乎有一个错字。 它应该是:
#define _CRTDBG_MAP_ALLOC
请注意前导下划线。 在VS2005 / 2008文档使用_CRTDBG_MAP_ALLOC
和VS2013文档引用_CRTDBG_MAP_ALLOC
以后。
您可以检查其他一些事项:
_CRTDBG_MAP_ALLOC
定义添加到stdafx.h
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.