Memory corruption

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

Re: Memory corruption

Post by elhobbs » Thu Sep 03, 2009 5:57 pm

BlueFrenzy wrote: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.
here is a sample for sizeof - again it is all computed at compile time

Code: Select all

typedef struct {
int a;
int b;
char c[32];
} sample_struct_t;


int main(int argc, char* argv[])
{
	sample_struct_t struct_array[25];
	sample_struct_t *ptr_sample_struct1 = struct_array;
	sample_struct_t *ptr_sample_struct2 = (sample_struct_t *)malloc(sizeof(sample_struct_t)*25);

	printf("sizeof(sample_struct_t) %d\n",sizeof(sample_struct_t));
	printf("sizeof(struct_array) %d\n",sizeof(struct_array));
	printf("sizeof(ptr_sample_struct1) %d\n",sizeof(ptr_sample_struct1));
	printf("sizeof(*ptr_sample_struct1) %d\n",sizeof(*ptr_sample_struct1));
	printf("sizeof(ptr_sample_struct2) %d\n",sizeof(ptr_sample_struct2));
	printf("sizeof(*ptr_sample_struct2) %d\n",sizeof(*ptr_sample_struct2));

	return 0;
}
results

Code: Select all

sizeof(sample_struct_t) 40
sizeof(struct_array) 1000
sizeof(ptr_sample_struct1) 4
sizeof(*ptr_sample_struct1) 40
sizeof(ptr_sample_struct2) 4
sizeof(*ptr_sample_struct2) 40

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

Re: Memory corruption

Post by BlueFrenzy » Mon Sep 07, 2009 10:39 am

Yeah, It uses the first example. It works fine in the computer.It's not a logic question, at least not in my code. Probably a malloc corruption or maybe something with ram to vram conversion.


uLib sets the pointes to null when sending the image to vram, and when unloading it, returns the pointer again (i wonder how this does work). Maybe the problem is there.

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

Re: Memory corruption

Post by elhobbs » Tue Sep 08, 2009 2:00 pm

BlueFrenzy wrote:Yeah, It uses the first example. It works fine in the computer.It's not a logic question, at least not in my code. Probably a malloc corruption or maybe something with ram to vram conversion.
if it crashes then you have a logic problem ;)
BlueFrenzy wrote: uLib sets the pointes to null when sending the image to vram, and when unloading it, returns the pointer again (i wonder how this does work). Maybe the problem is there.
are you storing addresses in vram? if the vram address you are writing to is not 4 byte aligned then it will not save correctly. the ds processors do not support unaligned reads. additionaly the vram does not support 8bit read/write.

eternity007
Posts: 1
Joined: Fri Oct 23, 2009 1:58 pm

Re: Memory corruption

Post by eternity007 » Fri Oct 23, 2009 2:07 pm

hi,

are you using __attribute__ ((aligned (xxx))) to align your structure???

Try just avoiding it, cause I had an same experience while porting some code.

Mostly, if you malloc some structure which is say 32 byte align, may cause crash. As I guess malloc use 4byte alignment.
Though 32 is a multiple number of 4, I have experienced this problem.

I am not sure about the exact reason behind this.

I was porting a code from WINCE to Android and I faced this problem. My structure was 32byte aligned but after malloc-ing memory was getting corrupted anywhere (everywhere).

If someone can explain the reason, I will be thankful.

Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests