Zalo DS Blog

Monday, November 24, 2008

Memory Leaks

When I finished Rokoban I realized that I didn't have anything to tell me how well or bad I was working with my memory resources. So, here I'll tell you what I did to have a little bit more of control.

First of all. What's a memory leak? Well, this

void MemoryLeaker()
{
int *leak = new int[99999999];
}


is a memory leak :D

When you start learning how to make programs in C++ you soon realize that memory leaks are the more annoying part. They always teach you the basic rule: "Everything that has a new must have a delete" (or is that from the Matrix movies?). That seems easy at first, but after years of C++ development I know that no matter how hard I try, I am going to create memory leaks, and you too :P Even if you try to avoid them using smart pointers you are never safe from them.

So, is there a way to avoid them? Yeah. There are a lot of ways. You can even create a garbage collector like Java. But there is a more interesting way. It is posible not to avoid them but to let our program tell us about them. And that way you can debug your application in debug mode and finally release it without any of this checks, making it as efficient and fast as posible.

So, what is the basic idea? C++ allows you to overload new and delete. You can overload them for a class or for the whole program. And this has a lot of advantages. You can reduce memory fragmentation and you can have control of what happens any time you request some memory from the heap and when you release it. So, basically you can implement a mechanism that everytime you reserve some memory it creates an entry on a table (for example) and everytime you realease it it deletes that entry from that table. At the end of your program all the entries in your table means memory leaks.

If you really want to know more about this take a look at this link http://entland.homelinux.com/blog/2008/08/19/practical-efficient-memory-management/. I was about to write my own memory manager for my engine. But I found that Visual C implements something very interesting for you. In fact, it all began with this link http://www.highprogrammer.com/alan/windev/visualstudio.html I really recommend its reading. Here I discovered that there is already an implemented debug heap I could use instead of writing my own. It is enought for me at the moment. I can only check memory leaks on my Windows release but because I usually develop everything in Windows and then recompile it using ZEngine for the rest of the platforms I can now be sure that the common part is memory leak free. There can be still problems with all the especific parts coded for each platform but I hardly make any memory allocation there.

I am finally doing what I found in this url http://www.cplusplus.com/forum/general/3526/:

This should go into every .cpp , after all header inclusions

#ifdef _WIN32
#ifdef _DEBUG
#include
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#define new new( _NORMAL_BLOCK, __FILE__, __LINE__)
#define malloc(s) _malloc_dbg(s, _NORMAL_BLOCK, __FILE__, __LINE__)
#endif
#endif


And then at the beggining of my app:

// Get current flag
int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
// Turn on leak-checking bit.
tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
// Set flag to the new value.
_CrtSetDbgFlag( tmpFlag );


If you do this as anilpanicker is showing then at the end of your program you will receive a very usefull output telling you where exactly you reserved some memory that was never released :D Using this I fixed a small memory leak in Rokoban, very very small, but still it was there. Now I can sleep much better.

There is still a lot of work to be done in my engine. I wish I had all the time in the world to code it but unfortunatelly I have other things to do. Anyway, knowing all those things is always really useful, you never know when you can need them. In the company when I am working at the moment they have their own memory manager implemented. More than that. They have their own asserts. I have learnt from them that if your program can't load a resource the best thing that it can do is telling you and stop. Only in debug mode, of course. This is much better ans easier that trying to make your game work without a graphic loaded of something like that. In your release you should have all your resources so it doesn't make sense to make it work without them :)

Hope any of what I have written will be useful. And take a look at all the links. They were really usefull for me.

Saturday, November 22, 2008

Downloads section updated

Hi... I have just moved the downloads to a new server... hopefully this will be working better. The only change is Rokoban for the DS wich is now a single file instead of nds+resources. I haven´t had enought time to test it but I can't find the old version so I decided to upload this one

Enjoy

Sunday, November 16, 2008

Getting into the Wii

Helloooo, everyone!!

I have finished and I am still alive... that is good!! What am I talking about? Yeah, good question!

I have spent the last three weeks porting ZEngine to the Wii :) I was in a hurry somehow because I wanted to finish something for the Tehskeen competition, so it has been a nightmare. Three weeks programming 12 hours a day, including weekends. First at job then at home... never again!!!!

What is the tehskeen competition you may be wondering. It is the first Wii homebrew competition. Take a look here and to take a look at the entries, this link You can take a look at all the games and vote Rokoban for winner :P

The Wii scene is amazing. I think that DS has stopped and lots of people are moving to the Wii, so now it seems to be the more active. The unnoficial SDK (devkitpro with libogc) allows you to do a lot of things and people are working everyday to release new versions lots of libraries, so I think it is a good a idea to move.

To be honest, I enjoy more developing for the DS. I think there is still a lack in the Wii documentation and somedays trying to do something can easily become hard. You need to look for samples, ask in forums, irc... etc

Cool! I am free! I am going to stop developing for a couple of months, I really need it. But in the meantime there is going to be a lot of time to post here all the features that I have been making to ZEngine (and belive me, there are a lot of them)

You can download Rokoban in the tehskeen competition website, I think I can't still upload it here. But there is a new version of Gea VS CoDOS (I hate it!!!) for the Wii in the downloads section or here

Have fun!