Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio memory leak detection not printing file name and line number

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?

like image 461
A. D. Avatar asked Jun 03 '15 11:06

A. D.


People also ask

How do I detect memory leaks in Visual Studio?

To find memory leaks and inefficient memory usage, you can use tools such as the debugger-integrated Memory Usage diagnostic tool or tools in the Performance Profiler such as the . NET Object Allocation tool and the post-mortem Memory Usage tool.

How detect memory leak in .NET application?

Usage of memory profiler to detect a memory leak DotMemory, SciTech Memory Profiler, and ANTS Memory Profiler are the most popular.NET memory profilers. If you have Visual Studio Enterprise, you may also use a "free" profiler. Memory profilers all work in the same way.


2 Answers

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.

like image 89
NathanOliver Avatar answered Nov 07 '22 08:11

NathanOliver


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; 
}
like image 2
Vmanani Avatar answered Nov 07 '22 08:11

Vmanani