Memory corruption
-
- Posts: 6
- Joined: Fri Aug 28, 2009 1:33 pm
Memory corruption
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
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
Re: Memory corruption
Ensure your malloc calls are returning without error (i.e. not null) before referencing the pointer it gives you.
EDIT: I think I've misread the post a bit, just ignore me and my first post here :)
Code: Select all
sometype_t *var = (sometype_t*) malloc(sizeof(sometype_t));
if (var == NULL)
printSomeErrorMessageOrSomething("malloc() failed");
Last edited by ritz on Fri Aug 28, 2009 9:15 pm, edited 1 time in total.
Re: Memory corruption
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.
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.
-
- Posts: 6
- Joined: Fri Aug 28, 2009 1:33 pm
Re: Memory corruption
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?
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
are you usingBlueFrenzy 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.
Code: Select all
defaultExceptionHandler();
-
- Posts: 6
- Joined: Fri Aug 28, 2009 1:33 pm
Re: Memory corruption
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
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 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?
-
- Posts: 6
- Joined: Fri Aug 28, 2009 1:33 pm
Re: Memory corruption
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
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.
-
- Posts: 6
- Joined: Fri Aug 28, 2009 1:33 pm
Re: Memory corruption
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.
Stack trace is discarded since I removed the most big structures. also created the static arrays but it still crashes.
Who is online
Users browsing this forum: Ahrefs [Bot] and 3 guests