Programming 2D Games

The official forum for "Programming 2D Games" the book by: Charles Kelly

It is currently Fri Feb 28, 2020 8:49 am

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: About memory management
PostPosted: Wed Jul 30, 2014 2:23 pm 
Offline

Joined: Sat Oct 12, 2013 4:48 pm
Posts: 44
Hi,

the book is great but still has a lot of topics missing.

I haven't red all of the book yet but could you tell us more about memory management and how you used it within your engine and which parts of the codes relates to memory management.

Kind regards

jc mazza.


Top
 Profile  
Reply with quote  
PostPosted: Thu Jul 31, 2014 12:57 pm 
Offline
Site Admin
User avatar

Joined: Sat Jan 28, 2012 4:36 pm
Posts: 554
A good question. I wish I could have had the time to add more content to the book but publishing deadlines prevented it.

It might be easiest to divide memory usage into two categories. Memory we reserve by using the new operator and memory reserved by DirectX. In an object oriented design, such as we use in the game engine, memory reserved in a class should be released in the class's destructor. The new operator returns the address of the reserved memory which we store in a pointer variable. To release the memory we use delete with the pointer. Pointer safety is important, we don't want to call delete on an unitialized pointer. To facilitate safe pointer deletion I created a series of function templates in the constants.h file. Note, the code in the book used macros for safe deletion but that was an oversight on my part. I believe function templates are a better approach.
Code:
// Safely delete pointer referenced item
template <typename T>
inline void safeDelete(T& ptr)
{
    if (ptr)
    {
        delete ptr;
        ptr = NULL;
    }
}

The deleteAll() function in game.cpp of the Spacewar game uses the safeDelete function template to release memory reserved by the game engine.

Code:
//=============================================================================
// Delete all reserved memory
//=============================================================================
void Game::deleteAll()
{
    releaseAll();               // call onLostDevice() for every graphics item
    safeDelete(audio);
    safeDelete(graphics);
    safeDelete(input);
    safeDelete(console);
    safeDelete(messageDialog);
    safeDelete(inputDialog);
    initialized = false;
}

If you use the new operator to reserve memory in your game code be sure to use safeDelete to release it.

The memory reserved by DirectX must also be released. This is normally memory required to hold textures. The TextureManager class in the game engine includes the onLostDevice function to release memory and the onResetDevice function to load the texture back into memory. One of the most common memory management issues I see in my student's code is failing to properly release and reload texture memory. A game that does not handle texture memory correctly will stop displaying graphics if it losses the graphics context. The simple way to test a game is, start the game and then do something that causes the graphics context to switch to another application. Pressing Ctrl+Alt+Delete will normally suffice. When we return to the game it should continue to display graphics as if nothing happened. If the games display is blank then it is not properly managing the graphics textures. The Spacewar game includes two functions for this purpose: releaseAll and resetAll. Make sure you have similar code in your game to release and reset your game's textures.

_________________
Professor Kelly


Top
 Profile  
Reply with quote  
PostPosted: Tue Aug 05, 2014 6:18 pm 
Offline

Joined: Sat Oct 12, 2013 4:48 pm
Posts: 44
What is the difference between the macros and tamplate and why do you think templates are a better approach?

PS: sorry for bombarding you with lots of questions and threads, I just get confused quite easily.


Top
 Profile  
Reply with quote  
PostPosted: Tue Aug 05, 2014 10:37 pm 
Offline
Site Admin
User avatar

Joined: Sat Jan 28, 2012 4:36 pm
Posts: 554
This Microsoft post does a good job of comparing macros and templates:

http://msdn.microsoft.com/en-us/library/aa903548(v=vs.71).aspx

I'm happy to answer all your questions.

_________________
Professor Kelly


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group