Yalantis/uCrop · error · CImgArgumentException

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

Error message

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

What it means

CImg<T>::get_shared_slices() returns a shared-memory image referencing slices z0..z1 of channel c0. It throws CImgArgumentException when beg > end, beg >= size(), or end >= size(), meaning the slice range is inverted or lies outside the instance's memory.

Solutions

  1. Ensure z0 <= z1 and z1 < img.depth(); clamp to depth-1 before the call.
  2. Check c0 < img.spectrum(); select a valid channel or append channels if needed.
  3. For 2D images (depth 1), call get_shared_slices(0,0,c0) or use get_shared_rows instead.
  4. Re-derive z indices from img.depth() at call time rather than cached geometry.

Example fix

// before
CImg<float> sl = vol.get_shared_slices(0, vol.depth());   // exclusive end
// after
unsigned z1c = std::min(z1, vol.depth() - 1);
CImg<float> sl = vol.get_shared_slices(z0, z1c, c);
Defensive patterns

Strategy: validation

Validate before calling

bool ok = z0 <= z1 && z1 < img.depth() && c0 < img.spectrum();

Try / catch

try { auto sl = img.get_shared_slices(z0, z1, c); }
catch (CImgArgumentException& e) { /* clamp z1 to depth-1 and retry */ }

Prevention

When it happens

Trigger: Calling img.get_shared_slices(z0,z1,c0) with z0 > z1, z1 >= img.depth(), or c0 >= img.spectrum(), so the end offset exceeds the total pixel count.

Common situations: Treating a 2D image (depth==1) as volumetric and requesting slice ranges; count-vs-index confusion (depth instead of depth-1); a channel index that is valid for RGBA data but not for the loaded grayscale image.

Related errors


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

Appendix: source

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

    }

    //! Return a shared-memory image referencing one row of the image instance \const.
    const CImg<T> get_shared_row(const unsigned int y0, const unsigned int z0=0, const unsigned int c0=0) const {
      return get_shared_rows(y0,y0,z0,c0);
    }

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

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

View on GitHub (pinned to f788b534b4)