Yalantis/uCrop · error · CImgInstanceException

_cimg_instance "max_min(): Empty instance."

Error message

_cimg_instance "max_min(): Empty instance."

What it means

Instance guard in CImg<T>::max_min(t&): the method returns references to the maximum and minimum pixels, but the instance has zero pixels, so those references cannot be produced. Standard empty-instance sentinel for statistics functions.

Solutions

  1. Verify the image is non-empty with is_empty() before calling max_min()
  2. Check that loading/assignment succeeded before statistics
  3. Correct ROI/crop math that yielded a zero-size image
  4. Catch CImgInstanceException around the call

Example fix

// before
T minv;
const T& maxv = img.max_min(minv);
// after
if (!img.is_empty()) {
  T minv;
  const T& maxv = img.max_min(minv);
}
Defensive patterns

Strategy: validation

Validate before calling

if (img.is_empty()) throw std::runtime_error("image empty before max_min()");

Type guard

bool usable = !img.is_empty() && img.size() > 0;

Try / catch

try { T minv; const T& maxv = img.max_min(minv); } catch (const CImgInstanceException& e) { /* handle empty */ }

Prevention

When it happens

Trigger: Calling CImg<T>::max_min(t& min_val) on an empty instance (is_empty() true): default construction, zero dimensions, or a failed load/transform leaving no data.

Common situations: Auto-contrast/threshold workflows on unloaded images; statistics on images produced by an earlier failed pipeline step.

Related errors


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

Appendix: source

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

        if (val<val_min) { val_min = val; ptr_min = ptrs; }
        if (val>val_max) { val_max = val; ptr_max = ptrs; }
      }
    }

    //! Return a reference to the minimum pixel value as well as the maximum pixel value \const.
    template<typename t>
    const T& min_max(t& max_val) const {
      return ((CImg<T>*)this)->min_max(max_val);
    }

    //! Return a reference to the maximum pixel value as well as the minimum pixel value.
    /**
       \param[out] min_val Minimum pixel value.
    **/
    template<typename t>
    T& max_min(t& min_val) {
      if (is_empty())
        throw CImgInstanceException(_cimg_instance
                                    "max_min(): Empty instance.",
                                    cimg_instance);
      const T *ptr_min, *ptr_max;
      _min_max(ptr_min,ptr_max);
      min_val = (t)*ptr_min;
      return (T&)*ptr_max;
    }

    //! Return a reference to the maximum pixel value as well as the minimum pixel value \const.
    template<typename t>
    const T& max_min(t& min_val) const {
      return ((CImg<T>*)this)->max_min(min_val);
    }

    //! Return the kth smallest pixel value.
    /**
       \param k Rank of the smallest element searched.
    **/

View on GitHub (pinned to f788b534b4)