Page 1 of 2

Memory corruption

Posted: Fri Aug 28, 2009 1:52 pm
by BlueFrenzy
Hello. First of all, sorry for my bad english, it's not my mother language:P. I am making a port of a game from PC to DS. This game works perfectly on PC with no crashes and at full speed but I am having some problems with ds, maybe due to DS limits.

This is the situation: the game loads fine, but after some time, it starts crashing. Most of the times after loading level 2.

There are 2 kind of crashes but I think they have the same origin. One is inside malloc. To be accurate, it's inside _malloc_r and game crashes completely. It is strange since if an error happens during the calling to malloc, it returns NULL pointer, instead, this makes the application to crash.

The other one seems by an unauthoriced access to the contento f a pointer. Just when I touch a coin in the game (it is destroyed and some spark object are created) the game freezes. I tried to search a bit the reason and it seems that when accessing a pointer when drawing, it crashes. the funny thing is that it is impossible that it has something uninitialized. I shall remember that the application works so fine on my computer.

Once I was able to stop and showing data before the application crashed. Data was corrupted, no number was right. I was lucky since I compared pointer to NULL to show that message, but it never catches it and the game crashes. I was very lucky because it seems that even pointers are corrupted and they point to somewhere else.

Also, before the chrash happens, everything else seems to work fine, and data is fine, AI is working, gfx are working, everything moves and plays well. Maybe pointers point to a forbidden address or unexistant and because of that it crashes randomply when I create something, or maybe creating something.

I do not use all the memory. Using a function that I found in uLib i read 2.2 Mb, so I should have enough space to work with. Is it possible that it have the pointers spread arround everywhere and then it doesn't find free memory and returns any pointer?

I am about changing all the dynamic lists to static arrays so I reduce the number of mallocs.

Any idea? :s thanks in advance :D

Re: Memory corruption

Posted: Fri Aug 28, 2009 3:27 pm
by ritz
Ensure your malloc calls are returning without error (i.e. not null) before referencing the pointer it gives you.

Code: Select all

sometype_t *var = (sometype_t*) malloc(sizeof(sometype_t));
if (var == NULL)
    printSomeErrorMessageOrSomething("malloc() failed");
EDIT: I think I've misread the post a bit, just ignore me and my first post here :)

Re: Memory corruption

Posted: Fri Aug 28, 2009 7:33 pm
by elhobbs
there are two areas that I have had problems with when porting from PC to DS:
1) stack overflow - the stack on the ds is tiny - only 16k by default - it really hard to increase as well as ill advised. the best solutionis to modify the code to use less stack space.
2) unaligned int and short data access. the code that I was porting was littered with packed structs such that ints and shorts were not 4 byte and 2 byte aligned respectively. the arm processors can not handle unaligned memory access - PCs do not have this issue.

Re: Memory corruption

Posted: Sun Aug 30, 2009 10:22 pm
by BlueFrenzy
Is it possible that an stack overflow exception cause memory corruption? It's possible since DS doesn't break when an exception occurs unlike in pc.

How could I help that? why the stack is filled? Maybe an excessive number of function callings with big struct instances? So, if it's an stack problem, then making static arrays instead of dynamic lists will do nothing, right?

Re: Memory corruption

Posted: Sun Aug 30, 2009 10:41 pm
by elhobbs
BlueFrenzy wrote:Is it possible that an stack overflow exception cause memory corruption? It's possible since DS doesn't break when an exception occurs unlike in pc.
are you using

Code: Select all

defaultExceptionHandler();
it will install an exception handler that dumps the stack and registers. it uses iprintf so you need to make sure that you have a console setup to see the output.

Re: Memory corruption

Posted: Mon Aug 31, 2009 12:25 pm
by BlueFrenzy
Yes, I do. I have some stack traces and some of them lead to _malloc_r tag. But nothing about stack trace. I only use pointers so I really doubt its a stack overflow problem. How why the byte alignment could fail?

Re: Memory corruption

Posted: Mon Aug 31, 2009 1:34 pm
by elhobbs
BlueFrenzy wrote:Yes, I do. I have some stack traces and some of them lead to _malloc_r tag. But nothing about stack trace. I only use pointers so I really doubt its a stack overflow problem. How why the byte alignment could fail?
one scenario is that you malloc a block of memory and store the returned pointer in a packed struct. if the address is not aligned (the address where you are storing the pointer - malloc always returns aligned pointers in libnds) then it will not be stored correctly.

Re: Memory corruption

Posted: Tue Sep 01, 2009 9:15 am
by BlueFrenzy
I understand. I am using sizeof() for retrieving the desired data. Maybe It doesn't get the right size? Also, then why it works at the start?

Re: Memory corruption

Posted: Tue Sep 01, 2009 1:26 pm
by elhobbs
I am not really sure what point you are trying to make. sizeof() only works at compile time. It cannot determine the size of a malloc'ed block of memory.

Re: Memory corruption

Posted: Thu Sep 03, 2009 12:10 pm
by BlueFrenzy
It should retrieve the size of the struct. But if it were worrupted, it'd never work, but it works for a while.

Stack trace is discarded since I removed the most big structures. also created the static arrays but it still crashes.