Posted by (twitter: @SirGFM)
August 22nd, 2015 12:02 pm

So… Just now, all of a sudden, my game wouldn’t run anymore. I though it was weird, since I shouldn’t have modified anything that would make things explode at random times… Well, since gdb wasn’t helping a lot, I tried to see how much free memory I still had… Just… look at the print screen:


125 MBs free…

Since it was the first I was streaming, chronolapse was on, two browsers were open, a few GIMP windows were also open and was coding my game, I blamed all the softwares (and not the stupid person that started ’em all) for the lack of memory. No big deal, after restarting the computer everything should be normal again…

Or not…

My game still wouldn’t run and gdb was still breaking in random places. Now I had to admit it was something on my code that was causing it. After trying to make in run again and fixing a unrelated bug, I noticed the it started to happen after I added a new “object”.

When I looked into its initialization function, it was quite clear what was wrong. This is how it looked at the time:

gfmRV mob_getNew(mob **ppMob) {
 gfmRV rv; 
 *ppMob = (mob*)malloc(sizeof(ppMob)); // <---- pay attention to this line
 rv = GFMRV_OK;
 return rv; 

Instead of alloc’ing enough memory for the object, I was only alloc’ing enough for the object’s pointer. So, when the game tried to update it, it would modify random places in the memory. This was the cause to the random crashes.

Well, lesson learned (once again). Coding in C can be *fun*.


3 Responses to “…”

  1. Chaoseed says:

    “Guys! Look what you’re doing here! You’re dereferencing a null pointer! Open your eyes!”
    –Bret ‘the Hitman’ hart

  2. rxi says:

    Are you using valgrind? Use valgrind!

Leave a Reply

You must be logged in to post a comment.

[cache: storing page]