16

I'm working on a game with SDL in Visual Studio 2010. I came across the _CrtDumpMemoryLeaks() macro and thought I'd give it a go. Invoking _CrtDumpMemoryLeaks() does print memory leaks to the output window, but it does not show where it happens.

I've read the MSDN article at Memory Leak Detection Enabling , and it explains that if I define _CRTDBG_MAP_ALLOC it should output the line number of the offending statement. This does not happen in my case. (I was however able to get it to work if I use malloc() directly -- not by using 'new').

The code:

#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    int *var = new int(5);

    _CrtDumpMemoryLeaks();

    return 0;
}

The output is the following:

Detected memory leaks!
Dumping objects ->
{58} normal block at 0x007D1510, 4 bytes long.
 Data: <    > 05 00 00 00 
Object dump complete.

If _CrtDumpMemoryLeaks() is unable to output line numbers when allocating using 'new' then suggestions for other ways to achieve similar behavior is appreciated.

DanielV
  • 1,396
  • 2
  • 26
  • 48
John
  • 2,551
  • 5
  • 30
  • 40

5 Answers5

9

That's an old version of Visual Leak Detector.

Try this: http://vld.codeplex.com/

Stianhn
  • 265
  • 2
  • 14
  • Thanks for pointing that out. I delete my answer in favor of yours. Also, you should write your answer more like an actual answer instead of a comment. – Björn Pollex Jul 08 '10 at 12:57
  • The new version of VLD worked as advertised and helped find a bunch of small memory leaks, complete with call stacks and line numbers. Hurray. – Jared Updike Mar 22 '11 at 00:42
  • d= (◕‿↼ ) Could find an active fork at https://github.com/Azure/vld (but the original was discontinued since 2017, but open-source is hard to kill). – Top-Master Mar 11 '21 at 09:43
9

When you define _DEBUG and include <crtdbg.h> you get an overloaded operator new which takes additional parameters which you can use to specify the file and line numbers in placement new expressions.

E.g.

int* p = new (_NORMAL_BLOCK, __FILE__, __LINE__) int(5);

You can wrap this in a conditionally defined macro, e.g.

#ifdef _DEBUG
#define DEBUG_NEW_PLACEMENT (_NORMAL_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_NEW_PLACEMENT
#endif

int* p = new DEBUG_NEW_PLACEMENT int(5);

While you do see people defining a macro new to completely hide this form client code, I do not personally recommend it as it breaks anything already intentionally using placement new and you have to make sure that any headers using placement new (such as many standard headers) are included before any header redefining new. This can make it easy to let some inline uses of new in header files slip through without being 'adjusted'.

CB Bailey
  • 648,528
  • 94
  • 608
  • 638
3

You might need these defines after your includes


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
ULysses
  • 980
  • 4
  • 9
  • 7
    First, this re-defines new which is a bit horrible and breaks any existing placement new uses; secondly it needs a valid definition for `DEBUG_NEW` which is only provided in a standard VC install in `afx.h` which is very much an MFC header. – CB Bailey Jul 08 '10 at 12:28
2

Check out the piece of code.

Overloading operator new and operator delete to log all memory allocations and deallocations

I identified my memory leaks using this method.

DumbCoder
  • 5,608
  • 3
  • 27
  • 40
2

The accepted answer by Charles Bailey requires that you change your source code and this is not necessary. If you are using new and delete (or the array versions), all you need to do is place this code snippet in the stdafx.h file of each of your projects (including any static or dynamic library dependencies), and it will then give a source file and line number attached to each leaked memory object:

#ifdef _DEBUG
   #ifndef DBG_NEW
      #define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
      #define new DBG_NEW
   #endif
#endif  // _DEBUG

This comes straight from Microsoft's webpage on the matter.

Special Sauce
  • 4,722
  • 2
  • 23
  • 29