-1

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!

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
  • 1
    "I'm making a program in C" - any reason to spam the C++ tag then? – too honest for this site Apr 16 '17 at 02:22
  • Casting `malloc` is bad - http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Ed Heal Apr 16 '17 at 02:27
  • Use debugger and get the value of stream.out. Check the value after compress(). Check that value inside the function. They all should be identical. – Nguai al Apr 16 '17 at 02:38
  • @EdHeal The casting may be bad, but no matter when the size passed to malloc is right. And, I use Visual Studio, i cast it because the compiler make an error if there are no cast. – Thanh Dang Apr 16 '17 at 05:49
  • 1
    "I use Visual Studio, i cast it because the compiler make an error if there are no cast.". That's because you are using a C++ compiler. If you code in C, usa a C compiler. – n. m. could be an AI Apr 16 '17 at 06:28

1 Answers1

0

Now i found what the problem is, my "decompress()" func write some bytes more than the size of the memory allocated by "malloc()" for output. It's not generated an error as i expected to be. Thanks @Nguai al very much for your comment. I tried to store the position of memory before process, modify the position when decompressing, and do the subtract. Then, i found that it larger than the size of memory i allocated before.