The memory allocation looks like this:
Code: Select all
book.page_order = malloc(sizeof(char*)*book.num_pages);
int i;
unzGoToFirstFile(book.zip_file);
int status = UNZ_OK;
for(i = 0; status == UNZ_OK; i++) {
char *block = init_block(1);
unzGetCurrentFileInfo(book.zip_file,NULL,block,1024,NULL,0,NULL,0);
book.page_order[i] = block;
status = unzGoToNextFile(book.zip_file);
}
Code: Select all
inline char* init_block(Uint16 kb) {
Uint32 loc, bytes = kb*1024;
char* block = malloc(sizeof(char)*bytes);
for(loc = 0; loc < bytes; loc++)
block[loc] = 0;
return block;
}
The freeing looks like this:
Code: Select all
void close_comic_book(comic_book book) {
if(book.type == comic_book_zip)
unzClose(book.zip_file);
# ifndef PSP
int i;
for(i = 0; i < book.num_pages; i++) {
if(book.page_order[i] == NULL) {
printf("Memory leak detected.");
next_line();
}
else free(book.page_order[i]); //This is the line that crashes.
}
# endif
free(book.page_order);
}
Is my init_block code buggy? Or could this be an error with PSPSDK?
Edit] Ah, I forgot to mention. The memory appears to get corrupted by the unzClose, but putting the free before the unzClose doesn't fix anything; it still crashes in the same place.
Code: Select all
(gdb) print *book->page_order
$4 = 0x8a21500 "Deadpool 01/Deadpool - 001 00fc.jpg"
(gdb) print book->page_order[1]
$5 = 0x8a21910 "Deadpool 01/Deadpool - 001 01.jpg"
(gdb) until
471 unzClose(book->zip_file);
(gdb) until
474 for(i = 0; i < book->num_pages; i++) {
(gdb) print book->page_order[1]
$6 = 0x8a21910 "Deadpool"
(gdb) print book->page_order[]
A syntax error in expression, near `]'.
(gdb) print book->page_order[0]
$7 = 0x8a21500 "Deadpool 01/\020\031¢\bpool - 001 00fc.jpg"