# Paul Nettle's memory tracker mmgr and C++11

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

## Recommended Posts

To remember where allocations and deallocations occured paul nettle redefine new and delete like this:

// ---------------------------------------------------------------------------------------------------------------------------------
// Variations of global operators new & delete
// ---------------------------------------------------------------------------------------------------------------------------------

void	*operator new(size_t reportedSize);
void	*operator new[](size_t reportedSize);
void	*operator new(size_t reportedSize, const char *sourceFile, int sourceLine);
void	*operator new[](size_t reportedSize, const char *sourceFile, int sourceLine);

#endif // _H_MMGR

// ---------------------------------------------------------------------------------------------------------------------------------
// Macros -- "Kids, please don't try this at home. We're trained professionals here." :)
// ---------------------------------------------------------------------------------------------------------------------------------

#include "nommgr.h"
#define	new		(m_setOwner  (__FILE__,__LINE__,__FUNCTION__),false) ? NULL : new
#define	delete		(m_setOwner  (__FILE__,__LINE__,__FUNCTION__),false) ? m_setOwner("",0,"") : delete
#define	malloc(sz)	m_allocator  (__FILE__,__LINE__,__FUNCTION__,m_alloc_malloc,sz)
#define	calloc(sz)	m_allocator  (__FILE__,__LINE__,__FUNCTION__,m_alloc_calloc,sz)
#define	realloc(ptr,sz)	m_reallocator(__FILE__,__LINE__,__FUNCTION__,m_alloc_realloc,sz,ptr)
#define	free(ptr)	m_deallocator(__FILE__,__LINE__,__FUNCTION__,m_alloc_free,ptr)


However, in C++11 you can delete constructors like this:

class TestClass
{
public:
TestClass() = delete; //Breaks the memory tracker!
TestClass(int i) { }
virtual ~TestClass() { cout << "Destructor called." << endl; }
};


Any idea how to modify the memory tracker to work with C++11? Wasn't it pretty damn stupid to choose "delete" for deleting constructors when it is already a keyword for freeing memory?

##### Share on other sites

Wasn't it pretty damn stupid to choose "delete" for deleting constructors when it is already a keyword for freeing memory?

I don't think it was really stupid. Relying on a macro override of a keyword isn't real safe to begin with. I believe the standard says you shouldn't do it. In this case, the keyword is now context sensitive. The pre-processor is incapable of determining the context.

##### Share on other sites

I can't think of any clean way to do it, but you can use a macro that evaluates to delete': [EDIT: This doesn't work, as someone pointed out below.]

#define cpp11_delete delete

// ...

class TestClass
{
public:
TestClass() = cpp11_delete;
// ...


You could also use a less hackish tool, like Valgrind or Purify.

Edited by Álvaro

##### Share on other sites

I can't think of any clean way to do it, but you can use a macro that evaluates to delete':

#define cpp11_delete delete

// ...

class TestClass
{
public:
TestClass() = cpp11_delete;
// ...

You could also use a less hackish tool, like Valgrind or Purify.

I tried it but cpp11_delete still evaluates to (m_setOwner (__FILE__,__LINE__,__FUNCTION__),false) ? m_setOwner("",0,"") : delete.

##### Share on other sites

#define calloc(sz)

I have no idea how that works, since calloc takes two parameters.

I dunno. Try making a nested macro that pastes together the tokens "del" and "ete"? It might not work, but then again, this is still an iffy practice.

##### Share on other sites

What difference does it make if you just don't use TestClass() = delete; ? I mean, i've written code just fine for years without ever using this and it work. What's the point of deleting a constructor? I don't get it.

EDIT: I think i understand now, it's to force using the constructor with the parameter isn't it?

Maybe this would work?

#ifdef _DEBUG
// ---------------------------------------------------------------------------------------------------------------------------------
// Variations of global operators new & delete
// ---------------------------------------------------------------------------------------------------------------------------------

void	*operator new(size_t reportedSize);
void	*operator new[](size_t reportedSize);
void	*operator new(size_t reportedSize, const char *sourceFile, int sourceLine);
void	*operator new[](size_t reportedSize, const char *sourceFile, int sourceLine);

// ---------------------------------------------------------------------------------------------------------------------------------
// Macros -- "Kids, please don't try this at home. We're trained professionals here." :)
// ---------------------------------------------------------------------------------------------------------------------------------

#include "nommgr.h"
#define	new	(m_setOwner (__FILE__,__LINE__,__FUNCTION__),false) ? NULL : new
#define	delete	(m_setOwner (__FILE__,__LINE__,__FUNCTION__),false) ? m_setOwner("",0,"") : delete
#define	malloc(sz)	m_allocator (__FILE__,__LINE__,__FUNCTION__,m_alloc_malloc,sz)
#define	calloc(sz)	m_allocator (__FILE__,__LINE__,__FUNCTION__,m_alloc_calloc,sz)
#define	realloc(ptr,sz)	m_reallocator(__FILE__,__LINE__,__FUNCTION__,m_alloc_realloc,sz,ptr)
#define	free(ptr)	m_deallocator(__FILE__,__LINE__,__FUNCTION__,m_alloc_free,ptr)
#endif

class TestClass
{
public:
#ifdef _DEBUG
TestClass();
#else
TestClass() = delete;
#endif
TestClass(int i) { }
virtual ~TestClass() { cout << "Destructor called." << endl; }
};

Edited by Vortez

##### Share on other sites
Every C++ game engine that I've worked with has simply banned the use of new/delete directly, and required all the code to use their own macro -- like #define MY_NEW new -- which lets you insert memory tracking without breaking anyone else's code.

If you're trying to track someone else's code, mmgr has always been very hacky and brittle, so using an external tool might be useful.

##### Share on other sites

Personally i just use a class that manage the allocation and deallocation automatically. That way, if you forget or dont care to release the memory, it will eventually be freed in the destructor. I dont get why i got downvoted, i just asked a question... And im the only one who posted a potential solution that work. You dont need this (the memory hack thingy) in a release build.

Edited by Vortez

##### Share on other sites
Following up on what I mentioned earlier. This is from a draft of C++11 standard, so I can't guarantee this is what the final version said, since I don't have a copy available.

17.6.4.3.1
Macro names
[macro.names]
2 A translation unit shall not #define or #undef names lexically identical to keywords, to the identifiers listed in Table3,or to the attribute tokens described in 7.6.

##### Share on other sites

Most of Paul Nettle's publicly released code comes from the late-90s, early 2000s IIRC. I guess you could say that particular piece of code didn't age very well, but at the same time you got it for free and didn't pay anything for using it, so I think on balance you've come out ahead.

Edited by Steve_Segreto

##### Share on other sites

And im the only one who posted a potential solution that work.

This is obviously just an example, where deleting the default constructor is superfluous. However, there's a difference between deleting an automatically generated function, and declaring, but never defining, a function. I don't think that's a drop-in replacement.

##### Share on other sites

As Hodgman says, every engine I've worked with does not use new and delete directly. Instead they define their own macro. Such as MY_NEW (using Hodgmans example).

So code then becomes:

MyObject *myObject = MY_NEW MyObject();


Where MY_NEW would be defined something like:

#if defined( _DEBUG )
#define MY_NEW    new( __FILE__, __LINE__ )
#else
#define MY_NEW    new

#endif //defined( _DEBUG )

Edited by nfactorial