Click here to Skip to main content
15,867,704 members
Articles / Programming Languages / VC++
Tip/Trick

Setting memory allocation break point in watch window

Rate me:
Please Sign up or sign in to vote.
4.67/5 (9 votes)
5 Feb 2012CPOL 29.3K   6   4
Memory leak detection in VC++
This tip helps to utilize the contex operator :
{,,msvcrxxd.dll}_crtBreakAlloc

Memory leaks in VC++ can be detected using debug heap function. To enable these, we should use:
C++
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

C++
// Note that in the above code, the order of #include statements must not change

And to track memory (leaks) allocated via 'new', we can use:
C++
#ifdef _DEBUG
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif

Now at the end of main() or at the point of interest in the code, we should call _CrtDumpMemoryLeaks();
C++
void main()
{
   // ....
  _CrtDumpMemoryLeaks();
}

The leaks, if any, are shown in the Output window as:
XML
C:\Projects\Sample\main.cpp(10) : {30} normal block at 0x00780E80, 64 bytes long.
 Data: <> CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD


We can utilize the memory allocation number (30 in the above example) to identify the code which caused the leak. To do this, we can use _crtBreakAlloc or the context operator in the watch window:
{,,msvcrxxd.dll}_crtBreakAlloc

Here xx should be replaced by the version of Visual Studio.
90 for VS2008
80 for VS2005 ...
In the watch window, the value of this wil be -1 initially and we can then set it to our memory allocation number to break at the appropiate code block.

To consolidate, all we have to do is this:
C++
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

void main()
{
   int* i = new int;
   // .....
   // the memory allocated with pointer i is not released and will be shown as leak
  _CrtDumpMemoryLeaks();
}


Hope now it is usable in the real world.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India
_____________________________________________________________

Did my masters from IIT-M in Advanced Manufacturing Technology and working mainly on C++ in CAD domain from 2004 onwards.
Working on web technologies using Angular 7.0 and above, HTML5, CSS3 from 2015.

Comments and Discussions

 
GeneralReason for my vote of 5 It's very useful, I will insert this... Pin
loggy066-Feb-12 20:52
loggy066-Feb-12 20:52 
GeneralI have done it. If this is what you want, try thinking of ch... Pin
Lakamraju Raghuram27-Jan-12 2:04
Lakamraju Raghuram27-Jan-12 2:04 
GeneralOh... you see lot of noise? I do not see that way. All you h... Pin
Lakamraju Raghuram27-Jan-12 1:59
Lakamraju Raghuram27-Jan-12 1:59 
GeneralReason for my vote of 1 Without some code that utilizes this... Pin
Paul Tait27-Jan-12 1:18
Paul Tait27-Jan-12 1:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.