Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 abrakadabra   Members   -  Reputation: 108

Like
0Likes
Like

Posted 13 April 2014 - 02:14 AM

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?



Sponsor:

#2 Rattrap   Members   -  Reputation: 1651

Like
4Likes
Like

Posted 13 April 2014 - 09:11 AM

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.

#3 Álvaro   Crossbones+   -  Reputation: 13292

Like
2Likes
Like

Posted 13 April 2014 - 09:17 AM

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, 14 April 2014 - 09:37 AM.


#4 abrakadabra   Members   -  Reputation: 108

Like
0Likes
Like

Posted 13 April 2014 - 10:58 AM

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



#5 Ectara   Crossbones+   -  Reputation: 2965

Like
0Likes
Like

Posted 13 April 2014 - 01:04 PM


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



#6 Vortez   Crossbones+   -  Reputation: 2698

Like
0Likes
Like

Posted 13 April 2014 - 04:29 PM

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, 13 April 2014 - 04:40 PM.


#7 Hodgman   Moderators   -  Reputation: 30350

Like
1Likes
Like

Posted 13 April 2014 - 04:35 PM

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.

#8 Vortez   Crossbones+   -  Reputation: 2698

Like
0Likes
Like

Posted 13 April 2014 - 04:46 PM

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, 13 April 2014 - 05:06 PM.


#9 Rattrap   Members   -  Reputation: 1651

Like
1Likes
Like

Posted 13 April 2014 - 08:01 PM

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.



#10 Steve_Segreto   Crossbones+   -  Reputation: 1530

Like
0Likes
Like

Posted 13 April 2014 - 09:14 PM

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, 13 April 2014 - 09:14 PM.


#11 Ectara   Crossbones+   -  Reputation: 2965

Like
0Likes
Like

Posted 13 April 2014 - 09:18 PM


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.



#12 nfactorial   Members   -  Reputation: 729

Like
1Likes
Like

Posted 14 April 2014 - 06:08 AM

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, 14 April 2014 - 06:08 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS