# Detecting Memory Leaks

This topic is 3353 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello everyone, i am trying to detect memory leaks in my code but i somehow cant make it fully work. Here is what i do as an example: #define _CRTDBG_MAP_ALLOC #include <stdlib.h> #include <crtdbg.h> int main(char* args){ _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); int* i = new int(); } and as the msdn states i should get an output like this: 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. But my output downt include the file-name and the line number the leak is. Anyone have a hint for me what i am missing in my code to make this work correctly? I am useing VS 2008 Professional edition. Thx a lot in advance!

##### Share on other sites
You only get the file name and line if you use malloc() or other CRT memory allocation functions directly. If you use new, you don't get that information.

##### Share on other sites
Thx :-)

Too bad would have been awesome:-)

##### Share on other sites
You can do this to get that information for use with new :

#include <crtdbg.h>#pragma warning(disable:4291)void* operator new(size_t size, const char * file, const int line){	return ::operator new(size, _NORMAL_BLOCK, file, line);}void* operator new[](size_t size, const char * file, const int line){	return ::operator new[](size, _NORMAL_BLOCK, file, line);}#ifdef _DEBUG#	define DBG_NEW new(__FILE__, __LINE__)#else#	define DBG_NEW new#endif

You have to replace new with DBG_NEW in your code, but it should at least help you track down the source of those leaks, then you can tidy the code back up afterwards.

On a side note, I know this probably isn't much use to you now but using smart pointers would mean you generally wouldn't have to worry about this kind of thing.