Paul
Paul

Reputation: 463

Trying to debug memory leaks using Visual Studio 2008 Heap Debugger

We're trying to track down memory leaks in our Visual Studio C++ application. The application is unmanaged. I've been trying to use the VS Heap Debugger, to display file locations of leaked memory.

I've been trying to use the techniques explained here (see: "What is the effect of using '_CRTDBG_MAP_ALLOC' on the C++ 'new' and 'delete' operators?"):

http://forums.codeguru.com/showthread.php?312742-Visual-C-Debugging-How-to-manage-memory-leaks

and here (see: "How to make it work vol. 2"):

http://msdn.microsoft.com/en-us/library/e5ewb1h3%28v=VS.80%29.aspx

I've defined the following macro:

#ifdef CRT_DEBUGHEAP_ENABLE
    #include <stdlib.h>
    #include <crtdbg.h>
    #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
    #define new DEBUG_NEW
#endif

The problem is that we using the Microsoft xtree VS code, which includes the following:

_Nodeptr _Buynode(_Nodeptr _Larg, _Nodeptr _Parg,
                  _Nodeptr _Rarg, const value_type& _Val, char _Carg)
{   // allocate a node with pointers, value, and color
    _Nodeptr _Wherenode = this->_Alnod.allocate(1);
    _TRY_BEGIN
        new (_Wherenode) _Node(_Larg, _Parg, _Rarg, _Val, _Carg);
    _CATCH_ALL
    this->_Alnod.deallocate(_Wherenode, 1);
    _RERAISE;
    _CATCH_END
    return (_Wherenode);
}

The new statement allocates the memory at the specific location _Wherenode, and the macro fails:

error C2061: syntax error : identifier '_Wherenode'

I've tried multiple variadic macro definitions, but these also fail.

Can anyone help?

Upvotes: 2

Views: 1288

Answers (1)

Captain Obvlious
Captain Obvlious

Reputation: 20093

The macro version of new prevents you from using placement new. The preprocessor expands the expression to the following

new(_NORMAL_BLOCK, __FILE__, __LINE__)(_Wherenode)

To work around this you need to undefine the macro before you include any header file that uses placement new.

#undef new
#include <xtree>
#define new DEBUG_NEW
#include <map>

Or a slightly safer way

#pragma push_macro("new")
#undef new
#include <xtree>
#pragma pop_macro("new")
#include <map>

Upvotes: 3

Related Questions