问题
I want to check my program for memory leaks and found this Microsoft article.
I thoroughly followed the article and added
#define CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
and
_CrtDumpMemoryLeaks();
when the program exits.
It properly dumps all the memory leak info in my output window, but here's the problem:
It doesn't print the file name and line number where the memory leaks are!
It says in the article that with #define _CRTDBG_MAP_ALLOC
it prints the file name and line number, but it doesn't for me.
My output looks like this
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
...
So I can't really work with that... also pressing F4 to go to the line doesn't work.
Could you please help me?
回答1:
You #define
is wrong. In order to get the format of
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.
You need to use:
#define _DEBUG
#define _CRTDBG_MAP_ALLOC
You have to include _DEBUG
as well since _CRTDBG_MAP_ALLOC
is only available with _DEBUG
defined(source).
Also from this answer make sure that the #define
is in the cpp file you want to check.
回答2:
I hope this helps if u havent figured out @A.D, works for win32 applications, we need to override the new operator. unfortunately it doesnot work for MFC application.:(
#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;
}
回答3:
Looks like there is a typo in the VS2013 documention sample at the top. It should be:
#define _CRTDBG_MAP_ALLOC
Note the leading underscore. The VS2005/2008 document use _CRTDBG_MAP_ALLOC
and the VS2013 document references _CRTDBG_MAP_ALLOC
later on.
A few other things you can check:
- Make sure you are in the Debug build
- Clean/rebuild your application
- If you are using a precompiled header make sure you add the
_CRTDBG_MAP_ALLOC
definition tostdafx.h
来源:https://stackoverflow.com/questions/30619395/visual-studio-memory-leak-detection-not-printing-file-name-and-line-number