Yalantis/uCrop · warning

get_serialize(): Failed to save compressed data, saving them

Error message

get_serialize(): Failed to save compressed data, saving them uncompressed.

What it means

In CImgList::get_serialize() with cimg_use_zlib enabled, the zlib compress() call returned a non-zero (error) status, so the warning is emitted and the data falls back to being stored uncompressed. The serialization still succeeds; only the compression of that item failed.

Source

Thrown at ucrop/src/main/jni/CImg.h:68759

      const char *const ptype = pixel_type(), *const etype = cimg::endianness()?"big":"little";
      cimg_snprintf(tmpstr,tmpstr._width,"%u %s %s_endian\n",_width,ptype,etype);
      CImg<ucharT>::string(tmpstr,false).move_to(stream);
      cimglist_for(*this,l) {
        const CImg<T>& img = _data[l];
        cimg_snprintf(tmpstr,tmpstr._width,"%u %u %u %u",img._width,img._height,img._depth,img._spectrum);
        CImg<ucharT>::string(tmpstr,false).move_to(stream);
        if (img._data) {
          CImg<T> tmp;
          if (cimg::endianness()) { tmp = img; cimg::invert_endianness(tmp._data,tmp.size()); }
          const CImg<T>& ref = cimg::endianness()?tmp:img;
          bool failed_to_compress = true;
          if (is_compressed) {
#ifdef cimg_use_zlib
            const ulongT siz = sizeof(T)*ref.size();
            uLongf csiz = (ulongT)compressBound(siz);
            Bytef *const cbuf = new Bytef[csiz];
            if (compress(cbuf,&csiz,(Bytef*)ref._data,siz))
              cimg::warn(_cimglist_instance
                         "get_serialize(): Failed to save compressed data, saving them uncompressed.",
                         cimglist_instance);
            else {
              cimg_snprintf(tmpstr,tmpstr._width," #%lu\n",csiz);
              CImg<ucharT>::string(tmpstr,false).move_to(stream);
              CImg<ucharT>(cbuf,csiz).move_to(stream);
              delete[] cbuf;
              failed_to_compress = false;
            }
#endif
          }
          if (failed_to_compress) { // Write in a non-compressed way
            CImg<charT>::string("\n",false).move_to(stream);
            stream.insert(1);
            stream.back().
              assign((unsigned char*)ref._data,ref._width,ref._height,ref._depth,ref._spectrum*sizeof(T),true);
          }
        } else CImg<charT>::string("\n",false).move_to(stream);

View on GitHub (pinned to f788b534b4)

Solutions

  1. Retry with is_compressed=false to guarantee serialization succeeds uncompressed.
  2. Free memory / process the data in smaller chunks (serialize per-image instead of a huge list).
  3. Inspect zlib's return code path and available memory; upgrade to 64-bit build if on 32-bit NDK.
  4. Verify the linked zlib is intact and headers match the library version.

Example fix

// before
CImg<unsigned char> buf = list.get_serialize(true);
// after
CImg<unsigned char> buf;
try { buf = list.get_serialize(true); }
catch (...) { buf = list.get_serialize(false); } // uncompressed fallback
Defensive patterns

Strategy: fallback

Validate before calling

// pre-check rough size before compressing
const unsigned long long nbytes = (unsigned long long)list.size() * sizeof(T);
if (nbytes > MAX_COMPRESS_BYTES) use_compression = false;

Try / catch

CImg<unsigned char> buf;
try { buf = list.get_serialize(true); }
catch (const std::bad_alloc&) { buf = list.get_serialize(false); }

Prevention

When it happens

Trigger: compress() fails during get_serialize(is_compressed=true), typically when the buffer to compress is huge and compressBound/allocation fails (out-of-memory for the destination buffer new Bytef[csiz] succeeding but compression failing), or zlib returning Z_STREAM_ERROR / Z_MEM_ERROR for the given sizes.

Common situations: Serializing extremely large images on memory-constrained devices (32-bit Android jni) where zlib cannot allocate internal state; corrupted zlib build; sizes exceeding ulongT/uLongf limits on 32-bit platforms.

Understand the failure class

Background: 'Something went wrong' / 'Request failed (500)' / 'HTTP error! status: 404' — what failed HTTP requests actually mean and how to find the real cause — this error's family across 28 libraries.

Related errors


AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08). Data as JSON: /api/errors/74d8cd4824174fdf. Report an issue: GitHub.