Sign in to follow this  
abrakadabra

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

Recommended Posts

abrakadabra    108

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?

Share this post


Link to post
Share on other sites
Rattrap    3385

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 this post


Link to post
Share on other sites
alvaro    21246

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 this post


Link to post
Share on other sites
abrakadabra    108

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

Share this post


Link to post
Share on other sites
Ectara    3097


#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 this post


Link to post
Share on other sites
Vortez    2714

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; }
    };
Edited by Vortez

Share this post


Link to post
Share on other sites
Hodgman    51231
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 this post


Link to post
Share on other sites
Vortez    2714

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 this post


Link to post
Share on other sites
Rattrap    3385
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 this post


Link to post
Share on other sites
Steve_Segreto    2080

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 this post


Link to post
Share on other sites
Ectara    3097


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 this post


Link to post
Share on other sites
nfactorial    735

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this