Yalantis/uCrop · error · CImgInstanceException

_cimg_instance "kth_smallest(): Empty instance."

Error message

_cimg_instance "kth_smallest(): Empty instance."

What it means

kth_smallest(k) throws CImgInstanceException when the instance is empty. It performs a selection algorithm over the pixel array to find the k-th smallest value, which is impossible without data. Note that k >= size() falls back to max(), so only the empty case throws here.

Solutions

  1. Guard with img.is_empty() before calling kth_smallest()
  2. Ensure the image contains data (size() > 0) before rank queries
  3. Fix the upstream operation that produced an empty image
  4. Catch CImgInstanceException to handle empty input gracefully

Example fix

// before
const T v = img.kth_smallest(10);
// after
if (!img.is_empty()) {
  const T v = img.kth_smallest(std::min<size_t>(10, img.size() - 1));
}
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: Calling CImg<T>::kth_smallest(k) with any k on an instance where is_empty() is true — no pixel buffer or zero-size dimensions.

Common situations: Percentile/threshold computation on images that failed to load; median-like filtering pipelines where an upstream step returned an empty image.

Related errors


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

Appendix: source

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

      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.
    **/
    T kth_smallest(const ulongT k) const {
      if (is_empty())
        throw CImgInstanceException(_cimg_instance
                                    "kth_smallest(): Empty instance.",
                                    cimg_instance);
      if (k>=size()) return max();
      CImg<T> arr(*this,false);
      ulongT l = 0, ir = size() - 1;
      for ( ; ; ) {
        if (ir<=l + 1) {
          if (ir==l + 1 && arr[ir]<arr[l]) cimg::swap(arr[l],arr[ir]);
          return arr[k];
        } else {
          const ulongT mid = (l + ir)>>1;
          cimg::swap(arr[mid],arr[l + 1]);
          if (arr[l]>arr[ir]) cimg::swap(arr[l],arr[ir]);
          if (arr[l + 1]>arr[ir]) cimg::swap(arr[l + 1],arr[ir]);
          if (arr[l]>arr[l + 1]) cimg::swap(arr[l],arr[l + 1]);
          ulongT i = l + 1, j = ir;
          const T pivot = arr[l + 1];
          for ( ; ; ) {

View on GitHub (pinned to f788b534b4)