• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
abrakadabra

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

11 posts in this topic

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?

0

Share this post


Link to post
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
2

Share this post


Link to post
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. sad.png

0

Share this post


Link to post
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.

0

Share this post


Link to post
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);
    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
0

Share this post


Link to post
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.
1

Share this post


Link to post
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
0

Share this post


Link to post
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.

1

Share this post


Link to post
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
0

Share this post


Link to post
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.

0

Share this post


Link to post
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
1

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  
Followers 0