I'm making a program in C for decode data. The data has compressed, so i tried to decompress data and success by having a correct result in decompressed. Then, i pass this decompressed data to other process. This process allocate new data, read from the one passed in, write to new data, and then free old data (because it'll not be used anymore). But, when run, it generate a error "decode.exe has triggered a break point" in the "free()" called line. My code is:
int decode (uint8_t* data, uint32_t size) {
compressed_stream stream;
stream.data = data;
stream.data_size = size;
uint32_t outsize;
//Compute output size and assign to outsize
//...
stream.out = (uint8_t*)malloc(outsize);
stream.size = outsize;
stream.inpos = stream.outpos = 0;
decompress(&stream);
//Not free stream.data yet
process(stream.out);
return 0; //Handling error later
}
"decompress()" function is tested, it do not tried to change the pointer to output data allocated. No error with my data, and the "outsize" computed equals to the needed size for output, "malloc()" return non-null data
And, process is as follow:
int process(uint8_t* data)
{
//Process data here, assign it to global scope pointers for easy handling
//...
free(data);
return 0;
}
Likewise, the processed data has tested and no error occur. But i crash when reach line "free(data)".
I tried not to use "process()" func and free the data associated with stream.out after "decompress()". But it still happened. But when i tried to change "free(data);" to:
realloc(data, 1);
free(data);
no error occur. Or, when i change the "outsize" the value which at much larger than needed for output of my decompressed data without realloc data, no error, too.
I dont know what happened with "malloc()" and "free()" func. My program works properly, it produce my needed data, but i want to optimized the memory used by it. Thanks for any help or explain!