Zlib deflate gives EXC_BAD_ACCESS error on Xcode

So, I'm building an application in Xcode that works great until I add the compression logic. Condensed code:

#include <zlib.h> 
....
    std::string str("dgfhsjhfhs");
    z_stream zs;
    zs.zalloc = Z_NULL;
    zs.zfree = Z_NULL;
    zs.opaque = Z_NULL;
    std::string s;
    int res = deflateInit(&zs, Z_BEST_COMPRESSION);
    if(res!=Z_OK)
        throw(std::runtime_error("deflateInit failed"));
    zs.next_in = (Bytef*)str.data();
    zs.avail_in = str.size();
    int ret;
    char out[32000];
    do
    {
        zs.next_out = reinterpret_cast<Bytef*>(out);
        zs.avail_out = sizeof(out);

        ret = deflate(&zs, Z_FINISH);

        if(s.size()<zs.total_out)
        {
            s.append(out, zs.total_out-s.size());
        }
    } while(ret == Z_OK);
    deflateEnd(&zs);

    if(ret!= Z_STREAM_END)
    {
        throw(std::runtime_error("Exception during compression!"));
    }
    printf("%s\n", s.c_str());

      

Then the code shows: EXC_BAD_ACCESS on deflateInit.

Could this be a problem due to multiple zlib.h files? Because when I search for it on my Mac, different files show up in different places.

+3


source to share





All Articles