Yalantis/uCrop · error · CImgArgumentException

save_cimg(): Specified filename is (null).

Error message

save_cimg(): Specified filename is (null).

What it means

CImgList::save_cimg delegates to _save_cimg, which requires either an open std::FILE* or a non-null filename. When both are null there is no output target, so a CImgArgumentException is thrown immediately. It protects the library from dereferencing a null path later during fopen.

Solutions

  1. Pass a valid non-null filename string, e.g. list.save_cimg("out.cimg").
  2. Check the filename for null/empty before calling and substitute a default output path.
  3. Open a std::FILE* yourself and use the _save_cimg/file overload if you need stream control.

Example fix

// before
const char *out = getenv("CIMG_OUT"); // may be NULL
list.save_cimg(out);
// after
const char *out = getenv("CIMG_OUT");
if (!out) out = "out.cimg";
list.save_cimg(out);
Defensive patterns

Strategy: validation

Validate before calling

if (!filename || !*filename) { filename = "out.cimg"; }
list.save_cimg(filename);

Type guard

bool hasPath(const char *p){ return p && *p; }

Try / catch

try { list.save_cimg(filename); } catch (CImgArgumentException &e) { std::fprintf(stderr, "no output file: %s", e.what()); }

Prevention

When it happens

Trigger: Calling save_cimg(nullptr) or save_cimg with a char* variable that is null at runtime (e.g. an unset filename from parsed options).

Common situations: Command-line parsing leaving the output filename unset; a function parameter forwarded straight through without a null check; refactoring from the FILE*-based overload to the filename overload and passing 0.

Related errors


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

Appendix: source

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

          }
        }
      }
      if (!file) cimg::fclose(nfile);
      return *this;
    }

    //! Save list into a .cimg file.
    /**
       \param filename Filename to write data to.
       \param is_compressed Tells if data compression must be enabled.
    **/
    const CImgList<T>& save_cimg(const char *const filename, const bool is_compressed=false) const {
      return _save_cimg(0,filename,is_compressed);
    }

    const CImgList<T>& _save_cimg(std::FILE *const file, const char *const filename, const bool is_compressed) const {
      if (!file && !filename)
        throw CImgArgumentException(_cimglist_instance
                                    "save_cimg(): Specified filename is (null).",
                                    cimglist_instance);
#ifndef cimg_use_zlib
      if (is_compressed)
        cimg::warn(_cimglist_instance
                   "save_cimg(): Unable to save compressed data in file '%s' unless zlib is enabled, "
                   "saving them uncompressed.",
                   cimglist_instance,
                   filename?filename:"(FILE*)");
#endif
      const char *const ptype = pixel_type(), *const etype = cimg::endianness()?"big":"little";
      std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
      const bool is_bool = ptype==cimg::type<bool>::string();
      std::fprintf(nfile,"%u %s %s_endian\n",_width,ptype,etype);

      cimglist_for(*this,l) {
        const CImg<T>& img = _data[l];
        std::fprintf(nfile,"%u %u %u %u",img._width,img._height,img._depth,img._spectrum);

View on GitHub (pinned to f788b534b4)