Jump to content
  • Advertisement
Sign in to follow this  
zike22

Memory problem

This topic is 4642 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The latest build of my game is suffering from some serious emory problems, I'm sure theres memory leaks everywhere. I'm not quite sure how it happen but i belive it was because i was constantly adding features to my game each build without there being any code to really really handel the memory. It was recomended to me that to fix my problem i would need to seperate game logic and draw logic, and have and a update_logic() and update_screen()function in the main, along with timers. My question is, what exactly defines game logic, im not sure how to put all my game logic in just one function. And how do i seperate my draw logic from my game logic, im very confused.

Share this post


Link to post
Share on other sites
Advertisement
Are you using C++? OOP? In that case, a good thing to remember is that everything you "new" in an object you should also "delete" in that object. Often you delete things in the deconstructor but data in the object can also be created and destroyed through-out the life of the object.

Share this post


Link to post
Share on other sites
You don't need to completely restructure your program. The process of tracking down and resolving the memory errors may well make you decide your program needs restructured, but that's a seperate decision.

What you do need is to do a walkthrough. You need to be able to annotate your source so you can keep track of what you've done and what you have yet to do. Personally, my preferance is a printout, but you could insert comments into the code and some editors will allow you to annotate code. It might not be a bad idea to keep notepad open to keep track of what you're working on currently.

You're looking for a specific problem. A walkthrough of the entire program wouldn't be a bad idea, but it isn't needed for this. You need to find all of the memory allocations. That's your starting point. You have a pointer to some memory so now what did you do with the pointer. You have to trace that pointer and all copies made of it.

One and only one of those pointers should lead you to a release of the memory. If none or more than one do then you have a problem. Lots of allocations, releases and copying of pointers so it can get confusing, but all you have to do is verify each allocation leads to one and only one release and you can do each of those independantly.

It may seem like a lot of work, but you aren't actually doing every allocation. Rather you are doing every statement that does an allocation. You're just debugging it. Done right it isn't hard to verify each allocation is released. If it is difficult then that's a sign of a problem with how you structured things. In those situations just step back, ask yourself what could have been done to make it easy to verify and whether you can just do that rather than verifying what you have.

Share this post


Link to post
Share on other sites
Well, if you're using VC++ you can use the CRT debugging facilities to make your job a alot easier.


// C-Runtime debugging support
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#endif
#include <crtdbg.h>

int main(int argc, char* argv[])
{
// If memory leak found, uncomment this and specify the memory
// allocation number to break on that allocation
//_CrtSetBreakAlloc(100);

// Your code here

// Print all memory leaks (including allocation number) to the
// VC++ debugger output
_CrtDumpMemoryLeaks();
return 0;
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!