Yalantis/uCrop · error · CImgArgumentException

get_shared_rows(): Invalid request of a shared-memory…

Error message

get_shared_rows(): Invalid request of a shared-memory subset (0->%u,%u->%u,%u,%u).

What it means

CImg<T>::get_shared_rows() returns a shared-memory image referencing rows y0..y1. It throws CImgArgumentException when the linear offsets are inverted (beg > end) or extend past the image size (beg >= size() or end >= size()), i.e. the requested row range does not fit inside the instance.

Solutions

  1. Ensure y0 <= y1 and y1 < img.height(); remember rows are inclusive bounds.
  2. Clamp: y0 = std::min(y0, img.height()-1); y1 = std::min(y1, img.height()-1);
  3. Check z0 < img.depth() and c0 < img.spectrum(), since bad z0/c0 also inflate the offset beyond size().
  4. Switch from get_shared_rows to a full copy (get_rows / crop) if you actually need non-contiguous or resized output.

Example fix

// before
CImg<float> rows = img.get_shared_rows(0, img.height());  // exclusive end
// after
CImg<float> rows = img.get_shared_rows(0, img.height() - 1, z, c);
Defensive patterns

Strategy: validation

Validate before calling

bool ok = y0 <= y1 && y1 < img.height() && z0 < img.depth() && c0 < img.spectrum();

Try / catch

try { auto rows = img.get_shared_rows(y0, y1, z, c); }
catch (CImgArgumentException& e) { /* clamp y1 to height-1 and retry */ }

Prevention

When it happens

Trigger: Calling img.get_shared_rows(y0,y1,z0,c0) with y0 > y1, or y1 >= img.height(), or z0/c0 out of range so the offset for row y1 exceeds size().

Common situations: Passing an exclusive end row (height instead of height-1); loop bounds computed with '<' semantics fed directly as inclusive bounds; channel/depth indices exceeding the image's spectrum or depth.

Related errors


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

Appendix: source

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

                                    cimg_instance,
                                    x0,x1,y0,z0,c0);
      return CImg<T>(_data + beg,x1 - x0 + 1,1,1,1,true);
    }

    //! Return a shared-memory image referencing a range of rows of the image instance.
    /**
       \param y0 Y-coordinate of the starting row.
       \param y1 Y-coordinate of the ending row.
       \param z0 Z-coordinate.
       \param c0 C-coordinate.
    **/
    CImg<T> get_shared_rows(const unsigned int y0, const unsigned int y1,
                             const unsigned int z0=0, const unsigned int c0=0) {
      const ulongT
        beg = (ulongT)offset(0,y0,z0,c0),
        end = (ulongT)offset(0,y1,z0,c0);
      if (beg>end || beg>=size() || end>=size())
        throw CImgArgumentException(_cimg_instance
                                    "get_shared_rows(): Invalid request of a shared-memory subset "
                                    "(0->%u,%u->%u,%u,%u).",
                                    cimg_instance,
                                    _width - 1,y0,y1,z0,c0);
      return CImg<T>(_data + beg,_width,y1 - y0 + 1,1,1,true);
    }

    //! Return a shared-memory image referencing a range of rows of the image instance \const.
    const CImg<T> get_shared_rows(const unsigned int y0, const unsigned int y1,
                                   const unsigned int z0=0, const unsigned int c0=0) const {
      const ulongT
        beg = (ulongT)offset(0,y0,z0,c0),
        end = (ulongT)offset(0,y1,z0,c0);
      if (beg>end || beg>=size() || end>=size())
        throw CImgArgumentException(_cimg_instance
                                    "get_shared_rows(): Invalid request of a shared-memory subset "
                                    "(0->%u,%u->%u,%u,%u).",
                                    cimg_instance,

View on GitHub (pinned to f788b534b4)