Memory corruption

BlueFrenzy
Posts: 6
Joined: Fri Aug 28, 2009 1:33 pm

Memory corruption

Post by BlueFrenzy » Fri Aug 28, 2009 1:52 pm

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

ritz
Posts: 24
Joined: Thu Jun 04, 2009 3:17 pm
Location: Canada

Re: Memory corruption

Post by ritz » Fri Aug 28, 2009 3:27 pm

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 :)
Last edited by ritz on Fri Aug 28, 2009 9:15 pm, edited 1 time in total.

elhobbs
Posts: 355
Joined: Thu Jul 02, 2009 1:19 pm

Re: Memory corruption

Post by elhobbs » Fri Aug 28, 2009 7:33 pm

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.

BlueFrenzy
Posts: 6
Joined: Fri Aug 28, 2009 1:33 pm

Re: Memory corruption

Post by BlueFrenzy » Sun Aug 30, 2009 10:22 pm

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?

elhobbs
Posts: 355
Joined: Thu Jul 02, 2009 1:19 pm

Re: Memory corruption

Post by elhobbs » Sun Aug 30, 2009 10:41 pm

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.

BlueFrenzy
Posts: 6
Joined: Fri Aug 28, 2009 1:33 pm

Re: Memory corruption

Post by BlueFrenzy » Mon Aug 31, 2009 12:25 pm

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?

elhobbs
Posts: 355
Joined: Thu Jul 02, 2009 1:19 pm

Re: Memory corruption

Post by elhobbs » Mon Aug 31, 2009 1:34 pm

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.

BlueFrenzy
Posts: 6
Joined: Fri Aug 28, 2009 1:33 pm

Re: Memory corruption

Post by BlueFrenzy » Tue Sep 01, 2009 9:15 am

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?

elhobbs
Posts: 355
Joined: Thu Jul 02, 2009 1:19 pm

Re: Memory corruption

Post by elhobbs » Tue Sep 01, 2009 1:26 pm

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.

BlueFrenzy
Posts: 6
Joined: Fri Aug 28, 2009 1:33 pm

Re: Memory corruption

Post by BlueFrenzy » Thu Sep 03, 2009 12:10 pm

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests