esavier
esavier

Reputation: 463

Zlib usage - deflateEnd() error

i am trying to compress block of strings in partial_flush mode, moreover there is one case when there is only one string to process.

Now i am calling deflateInit2(params...), deflate() and deflateEnd(). I get some correct output in output, witch is uncompressable(tried), but there is an error when trying to deallocate all memory using deflateEnd(&strm); or inflateEnd(&strm);

For now, i am thinking here will be no possibility to create this case on application run, but i need to pinpoint and eliminate error with memory leak.

whole schematis is like this:

class Czlib{
    compress( std::string );
    decompress( std::string );
    Czlib() // allocate inflate and deflate state here
   ~Czlib()// deallocate both here
}

int main(){
    for (char c=0x00 ; ; c++){
        std::string str(255, c);
        Czlib zlib;
        zlib.compress(str);
}

I know that class should die after each loop and probably it is, but deflateEnd and inflateEnd keeps reporting Z_DATA_ERROR, so in the end dynamically allocated data stays in memory :(

Upvotes: 1

Views: 2395

Answers (1)

Mark Adler
Mark Adler

Reputation: 112422

deflateEnd() returning Z_DATA_ERROR means that the deflate operations were left in some intermediate state, i.e. not completed, at the time of that call. So deflate() had never returned a Z_STREAM_END.

Regardless, all of the allocated memory for deflate is released by deflateEnd(). So if you're calling deflateEnd(), there will be no memory leak for memory allocated by zlib.

To properly complete the deflate stream, you need to give deflate() the Z_FINISH flush parameter, and then call deflate() to consume its output until it returns Z_STREAM_END.

Upvotes: 3

Related Questions