Yalantis/uCrop · error · CImgInstanceException

save_cimg(): Empty instance, for file

Error message

save_cimg(): Empty instance, for file '%s'.

What it means

save_cimg refuses to write when the CImgList instance itself is empty (zero images or all images zero-sized). It throws CImgInstanceException naming the target file. Writing an empty list would produce a meaningless or unparseable .cimg payload, so it is rejected up front.

Solutions

  1. Check list.size() (and per-image dimensions) before saving and populate the list if empty.
  2. Fix the upstream load/generation step that produced an empty list.
  3. Skip the save and log/skip gracefully when the list is legitimately empty.

Example fix

// before
list.save_cimg("out.cimg"); // list empty
// after
if (list.size() > 0) list.save_cimg("out.cimg");
else cimg::warn("Nothing to save to out.cimg");
Defensive patterns

Strategy: validation

Validate before calling

if (list.size() == 0) { /* skip or populate */ } else list.save_cimg(path);

Type guard

bool isSavable(const CImgList<T> &l){ return l.size() > 0; }

Try / catch

try { list.save_cimg(path); } catch (CImgInstanceException &e) { std::fprintf(stderr, "empty list: %s", e.what()); }

Prevention

When it happens

Trigger: Calling save_cimg on a default-constructed CImgList, on a list whose images were all assigned/destroyed, or after load failure left the list empty.

Common situations: Chained load->save where the load silently failed; filtering the list down to zero images before saving; forgetting to call assign()/insert() to populate the list.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

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

                  if (skipye) cimg::fseek(nfile,skipye,SEEK_CUR); \
                } \
                const unsigned int skipze = (D - 1 - nz1)*W*H*sizeof(Tss); \
                if (skipze) cimg::fseek(nfile,skipze,SEEK_CUR); \
              } \
              const unsigned int skipve = (C - 1 - nc1)*W*H*D*sizeof(Tss); \
              if (skipve) cimg::fseek(nfile,skipve,SEEK_CUR); \
            } \
          } \
        } \
        saved = true; \
      }

      if (!file && !filename)
        throw CImgArgumentException(_cimglist_instance
                                    "save_cimg(): Specified filename is (null).",
                                    cimglist_instance);
      if (is_empty())
        throw CImgInstanceException(_cimglist_instance
                                    "save_cimg(): Empty instance, for file '%s'.",
                                    cimglist_instance,
                                    filename?filename:"(FILE*)");

      std::FILE *const nfile = file?file:cimg::fopen(filename,"rb+");
      bool saved = false, endian = cimg::endianness();
      CImg<charT> tmp(256), str_pixeltype(256), str_endian(256);
      *tmp = *str_pixeltype = *str_endian = 0;
      unsigned int j, N, W, H, D, C;
      int i, err;
      j = 0; while ((i=std::fgetc(nfile))!='\n' && i!=EOF && j<256) tmp[j++] = (char)i; tmp[j] = 0;
      err = cimg_sscanf(tmp,"%u%*c%255[A-Za-z123468_]%*c%255[sA-Za-z_ ]",&N,str_pixeltype._data,str_endian._data);
      if (err<2) {
        if (!file) cimg::fclose(nfile);
        throw CImgIOException(_cimglist_instance
                              "save_cimg(): CImg header not found in file '%s'.",
                              cimglist_instance,
                              filename?filename:"(FILE*)");

View on GitHub (pinned to f788b534b4)