Yalantis/uCrop · error · CImgInstanceException

_cimg_instance "min(): Empty instance."

Error message

_cimg_instance "min(): Empty instance."

What it means

CImg<T>::min() returns a reference to the minimum pixel value, but this requires at least one pixel. Calling it on an empty instance (width/height/depth/spectrum of 0) would dereference null memory, so CImgInstanceException is thrown.

Source

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

       \param expression Math formula as a C-string.
       \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by
       \f$\mathrm{maxabs}(I_{(x,y,z,c)},\mathrm{expr}_{(x,y,z,c)})\f$.
    **/
    CImg<T>& maxabs(const char *const expression) {
      return maxabs((+*this)._fill(expression,true,3,0,"maxabs",this,0));
    }

    //! Pointwise maxabs operator between an image and an expression \newinstance.
    CImg<Tfloat> get_maxabs(const char *const expression) const {
      return CImg<Tfloat>(*this,false).maxabs(expression);
    }

    //! Return a reference to the minimum pixel value.
    /**
     **/
    T& min() {
      if (is_empty())
        throw CImgInstanceException(_cimg_instance
                                    "min(): Empty instance.",
                                    cimg_instance);
      T *ptr_min = _data;
      T min_value = *ptr_min;
      cimg_for(*this,ptrs,T) if (*ptrs<min_value) min_value = *(ptr_min=ptrs);
      return *ptr_min;
    }

    //! Return a reference to the minimum pixel value \const.
    const T& min() const {
      if (is_empty())
        throw CImgInstanceException(_cimg_instance
                                    "min(): Empty instance.",
                                    cimg_instance);
      const T *ptr_min = _data;
      T min_value = *ptr_min;
      cimg_for(*this,ptrs,T) if (*ptrs<min_value) min_value = *(ptr_min=ptrs);
      return *ptr_min;

View on GitHub (pinned to f788b534b4)

Solutions

  1. Check img.is_empty() (or img.size()>0) before calling min()
  2. Fix the upstream load/crop that produced the empty image
  3. Use the const-safe stats variants with an emptiness guard

Example fix

// before
float v = img.min();
// after
if (!img.is_empty()) { float v = img.min(); } else { /* handle empty */ }
Defensive patterns

Strategy: validation

Validate before calling

if (!img.is_empty()) { ... img.min() ... }

Try / catch

try { T v = img.min(); } catch (const CImgInstanceException& e) { /* handle empty image */ }

Prevention

When it happens

Trigger: Calling img.min() on a default-constructed CImg, an image assigned() to empty, or an image whose data failed to load.

Common situations: Reading an image file that failed to load silently; slicing/cropping that produced a 0-size result; running stats on an optional image that was never initialized.

Understand the failure class

Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.

Related errors


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