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

Started by
10 comments, last by nfactorial 10 years ago

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);
void	operator delete(void *reportedAddress);
void	operator delete[](void *reportedAddress);

#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?

Advertisement

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.

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

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.

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. sad.png


#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.

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);
    void	operator delete(void *reportedAddress);
    void	operator delete[](void *reportedAddress);
     
    // ---------------------------------------------------------------------------------------------------------------------------------
    // 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; }
    };
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.

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.

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.

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

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.

This topic is closed to new replies.

Advertisement