• 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
Ludus

Memory Management

16 posts in this topic

I am setting up a simple memory management in my program and I would like some feedback.

 

During the "loading" portion of my program I use something like this to create the dynamically loaded objects:

 

new CObject(0, 0, 1);
new CObject(0, 6, 4);
new CObject(21, 0, 21);

 

As you can see, I do not assign the address of any of these objects to pointers - I just create the objects and assign their constructor values. However, the constructor of "CObject" contains this bit of code:

 

ObjectList.push_back(this);
 


This places the address of the object in ObjectList. "ObjectLIst" is a vector of pointers for CObject, and it exists within the global scope of the program. Now during the "clean-up" phase of my program I use this to delete the allocated memory:

 

  for(int i = 0;i < CObject::ObjectList.size();i++) {
        if(!CObject::ObjectList[i]) continue;

        delete CObject::ObjectList[i];
    }

 CObject::ObjectList.clear();
 

 

I should mention that CObject is always created dynamically during the "loading" phase. It is never created anywhere else in the program.

 

Is there anything wrong with handling memory in this manner? Am I overlooking any potential problems with this method?

 

 

 

Edit: Just a note for future readers - I've realized that this is bad coding practice for the following reason: an object should not manage its own memory. That is the responsibility of the memory manager.

Edited by Ludus
0

Share this post


Link to post
Share on other sites

Is there anything wrong with handling memory in this manner? Am I overlooking any potential problems with this method?

 

Yes. You are violating all sorts of good coding practices.

 

First of all, the fact that you're using a global suggests there's other serious issues with your code.

 

Second of all, you're requiring that CObjects know about ObjectList for no good reason. Someday you will want to make a CObject without adding it to ObjectList and then you're design is totally screwed and you'll have to redo your design anyway. Don't think that will happen, do you? Well, the entire history of software development says otherwise. Since the only change to your code to *not* do that is just

ObjectList.push_back( new CObject(0, 0, 1) ); 

I don't see what you think your code gains you. Of course the above code isn't really safe either, in the event of an exception occurring.

 

I'm also curious over whether CObject even needs to be dynamically allocated like that. What does CObject look like? There might be a much better solution for your needs.

 

Side note: You don't need to check if for the pointer being non-null. C++ as defined considers delete 0; perfectly legal and safe. It contains the check for being null already.

0

Share this post


Link to post
Share on other sites

The main problem you'll have with this is exception safety.

 

Yes, exception handling is something I did not include here. I will be sure to include it in the final implementation. Thanks for the reminder.

 

First of all, the fact that you're using a global suggests there's other serious issues with your code.


Second of all, you're requiring that CObjects know about ObjectList for no good reason.

 

A detail I forgot to mention here is that ObjectList is not just used for memory management - it's accessed by a number of different functions throughout the program. CObjects need to know about this list because they need to access other objects in the list directly. For these reasons I decided to make ObjectList within the global scope.

 

Someday you will want to make a CObject without adding it to ObjectList and then you're design is totally screwed and you'll have to redo your design anyway. Don't think that will happen, do you? Well, the entire history of software development says otherwise. Since the only change to your code to *not* do that is just

ObjectList.push_back( new CObject(0, 0, 1) );

 

Because of the way CObjects work, there won't ever be a time when I'd want to make a CObject without adding it to ObjectList (the only way CObjects are of any use is by the rest of the program accessing them through this list - CObjects never do anything on their own). However, the code you gave does make a bit more sense so I may use it.

 

I don't see what you think your code gains you.

I'm also curious over whether CObject even needs to be dynamically allocated like that. What does CObject look like? There might be a much better solution for your needs.

 

The purpose of the way I'm handling memory is to make it very easy to make CObjects within the loading phase. During the "loading" phase, there will be many calls for "new CObject()", which is why I want it to be as simple of a statement as possible. CObjects mostly contain variables (as well as a few functions for calculations) that other parts of the program need to access.

0

Share this post


Link to post
Share on other sites

Is there anything wrong with handling memory in this manner? Am I overlooking any potential problems with this method?

Your code made me want to cry.  Where did you learn to manage your object memory like this?  Is this just something you came up with? I honestly want to know.

 

As mentioned, it violates several good coding practices.

0

Share this post


Link to post
Share on other sites

You should minimize surprises. If I join your project and I try to do something else with a CObject, the program will probably work fine and then crash in the cleanup phase, because it will either try to delete a pointer that I already deleted, or an invalid pointer to something that was once in the stack. Then I'll waste an hour figuring it out. If I find enough of these strange things, I will stop working on your project, because they are very frustrating.

2

Share this post


Link to post
Share on other sites

Instead of doing what you're doing there, just make the list hold smart pointers. That will save you from worrying about deleteing the memory and will garuntee exception safety.

2

Share this post


Link to post
Share on other sites

Instead of doing what you're doing there, just make the list hold smart pointers. That will save you from worrying about deleteing the memory and will garuntee exception safety.

 
That will still leave you with a class that cannot be used in the usual ways, because its objects self-register into some global container. Making the code to register these objects marginally shorter is a bad idea if you make your code harder to understand in the process.
 
I would either create a fuinction that creates the new object and registers it, or I would simply write this:
ObjectList.push_back(CObject(0, 0, 1)); // ObjectList should contain objects, not pointers, unless you have a good reason to need pointers
ObjectList.push_back(CObject(0, 6, 4));
ObjectList.push_back(CObject(21, 0, 2));
Edited by Álvaro
1

Share this post


Link to post
Share on other sites

Another thing you should probably know: "delete nullptr;" is a safe operation. You don't need to check for nullptr before invoking delete.

I didn't know that huh.png  Thanksrolleyes.gif

0

Share this post


Link to post
Share on other sites

Instead of doing what you're doing there, just make the list hold smart pointers. That will save you from worrying about deleteing the memory and will garuntee exception safety.

 
That will still leave you with a class that cannot be used in the usual ways, because its objects self-register into some global container. Making the code to register these objects marginally shorter is a bad idea if you make your code harder to understand in the process.
 
I would either create a fuinction that creates the new object and registers it, or I would simply write this:
ObjectList.push_back(CObject(0, 0, 1)); // ObjectList should contain objects, not pointers, unless you have a good reason to need pointers
ObjectList.push_back(CObject(0, 6, 4));
ObjectList.push_back(CObject(21, 0, 2));

 

I'm guessing CObject is a base class of some sort (I'm referring to the push_back solution, which would otherwise be my choice as well).

Edited by achild
0

Share this post


Link to post
Share on other sites

I'm guessing CObject is a base class of some sort (I'm referring to the push_back solution, which would otherwise be my choice as well).

 

I don't think so. His code with `new CObject(0, 0, 1);' doesn't allow for a derived class to be instantiated.

0

Share this post


Link to post
Share on other sites

I decided to use the following code to add an object to the list, rather than adding it using the object's constructor:

 

ObjectList.push_back(new CObject(0, 0, 1)); 
 

 

Thanks for all the feedback.

Edited by Ludus
1

Share this post


Link to post
Share on other sites

I decided to use the following code to add an object to the list, rather than adding it using the object's constructor:

 

ObjectList.push_back(new CObject(0, 0, 1)); 
 

 

Thanks for all the feedback.

 

Could you explain why you are not following my recommendation to make ObjectList be a container of objects instead of a container of pointers?

0

Share this post


Link to post
Share on other sites


Could you explain why you are not following my recommendation to make ObjectList be a container of objects instead of a container of pointers?

 

In my program, a single object sometimes needs to have its address in two different lists at the same.

Edited by Ludus
0

Share this post


Link to post
Share on other sites


Could you explain why you are not following my recommendation to make ObjectList be a container of objects instead of a container of pointers?

 

In my program, a single object sometimes needs to have its address in two different lists at the same.

In other words, you have to deal with ownership.  I suggest you look into smart pointers, shared_ptr and weak_ptr.

2

Share this post


Link to post
Share on other sites

In other words, you have to deal with ownership. I suggest you look into smart pointers, shared_ptr and weak_ptr.

 

I'll look into those. Thanks.

1

Share this post


Link to post
Share on other sites

In other words, you have to deal with ownership. I suggest you look into smart pointers, shared_ptr and weak_ptr.

 

I'll look into those. Thanks.

 

Just go through this: http://www.informit.com/articles/article.aspx?p=1944072

// a chapter from this, a great book, BTW: http://www.informit.com/store/c-plus-plus-primer-9780321714114

 

And then this:

http://herbsutter.com/2013/05/29/gotw-89-solution-smart-pointers/
http://herbsutter.com/2013/05/30/gotw-90-solution-factories/
http://herbsutter.com/2013/06/05/gotw-91-solution-smart-pointer-parameters/

 

Should be all you need to know to get started and do it right! :-)

Edited by Matt-D
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