I noticed that there are something wrong with malloc() etc. functions. Seems that sometimes free() does not free the memory.
I wrote this little program to demonstrate it:
Code: Select all
#include <stdio.h>
#include <malloc.h>
int main () {
int iter = 0;
realloc(malloc(10), 8); /* 1 */
realloc(malloc(10), 8); /* 2 */
realloc(realloc(malloc(10), 20), 10);
for (;;) {
char *ptr;
ptr = malloc(2048*1024); /* This can be smaller also */
printf ("%d: %p\n", iter++, ptr);
if (ptr == NULL) {
printf ("malloc() failed!\n");
break;
}
free(ptr);
}
return 0;
}
I'll compile it like this:
ee-gcc -DPS2_EE -O3 -G0 -Wall -I$PS2SDK/common/include -I$PS2SDK/ee/include -c main.c
ee-gcc -nostartfiles -L$PS2SDK/ee/lib -T$PS2SDK/ee/startup/linkfile -o test.elf $PS2SDK/ee/startup/crt0.o main.o -lc -lkernel -lsyscall -lc
And then run it:
# ps2client execee host:test.elf
loadelf: fname host:test.elf secname all
Input ELF format filename = host:test.elf
0 00100000 000040b8 .
Loaded, host:test.elf
start address 0x100008
gp address 00000000
0: 1079296
1: 3176464
2: 5273632
3: 7370800
4: 9467968
5: 11565136
6: 13662304
7: 15759472
8: 17856640
9: 19953808
10: 22050976
11: 24148144
12: 26245312
13: 28342480
14: 30439648
15: 0
malloc() failed!
After 15 malloc() and free()'s it has filled up the whole memory and malloc() returns NULL. According my testings, realloc() is needed to make free() behave like this. If you'll remove the lines commented with 1 and 2, malloc() and free() works normally and the loop does never end. Any ideas how to fix it? :)