Click here to Skip to main content
Click here to Skip to main content

Setting memory allocation break point in watch window

By , 5 Feb 2012
 
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:
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
// 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:
#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();
void main()
{
   // ....
  _CrtDumpMemoryLeaks();
}
The leaks, if any, are shown in the Output window as:
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:
#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)

About the Author

Lakamraju Raghuram
Software Developer Triad [ CADCAM-e ]
India India
Member
_____________________________________________________________
 
Did my masters from IIT-M in Advanced Manufacturing Technology and I am working mainly on C++ from 2004 onwards.
 
I like to play with my 16 month old daughter Pranavi [rather she plays with me]. By the way she pronounces C++ as "Chi pee-pee".
 
Hate traveling (but thanks to my wife I traveled to Hyd about 50 times in last 3 years).

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralI have done it. If this is what you want, try thinking of ch...memberLakamraju Raghuram27 Jan '12 - 2:04 
I have done it.
If this is what you want, try thinking of change you vote (:-

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 5 Feb 2012
Article Copyright 2012 by Lakamraju Raghuram
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid