I don't understand how the freeing is working. I know this happens when I try to free memory twice. However, this is really stumping me.
I've tried to post just the relevant parts of the code.
FILE* file = fopen(path, "r");
if (file == NULL)
{
error(500);
return;
}
// load file's content
BYTE* content;
size_t length;
if (load(file, &content, &length) == false)
{
error(500);
return;
}
This is the load fucntion
bool load(FILE* file, BYTE** content, size_t* length)
{
printf("\nLOAD STARTED\n");
content = NULL;
BYTE *data = NULL;
int size = 0;
while(!feof(file))
{
char ch = fgetc(file);
size += 1;
data = realloc(data, sizeof(BYTE) * (size));
*(data + (size - 1)) = ch;
}
content = &data;
*length = size;
printf("\nLOAD ENDED\n");
return true;
}
A little while later I'm calling free()
printf("\nFREEING CONTENT\n");
// free file's content
free(content);
printf("\nCONTENT FREED\n");
The printf statement FREEING CONTENT
works after which I get the
munmap_chunk(): invalid pointer
error.