Yalantis/uCrop · error · CImgArgumentException

assign(): Invalid assignment request of shared instance from

Error message

assign(): Invalid assignment request of shared instance from (%s*) buffer (pixel types are different).

What it means

CImg::assign(values,...) with is_shared=true tried to attach a shared image instance to a foreign memory buffer, but the buffer's pixel type t differs from the image's T, so the shared buffer cannot be reinterpreted as the image's pixel type. CImg refuses rather than silently misinterpreting the memory. Shared instances must have the exact same pixel type as the buffer they share.

Source

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

          _width = _height = _depth = _spectrum = 0; _data = 0;
          throw CImgInstanceException(_cimg_instance
                                      "assign(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).",
                                      cimg_instance,
                                      cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c),
                                      size_x,size_y,size_z,size_c);
        }
        std::memcpy((void*)new_data,(void*)values,siz*sizeof(T));
        delete[] _data; _data = new_data; _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c;
      }
      return *this;
    }

    //! Construct image with specified size and initialize pixel values from a memory buffer \overloading.
    template<typename t>
    CImg<T>& assign(const t *const values, const unsigned int size_x, const unsigned int size_y,
                    const unsigned int size_z, const unsigned int size_c, const bool is_shared) {
      if (is_shared)
        throw CImgArgumentException(_cimg_instance
                                    "assign(): Invalid assignment request of shared instance from (%s*) buffer "
                                    "(pixel types are different).",
                                    cimg_instance,
                                    CImg<t>::pixel_type());
      return assign(values,size_x,size_y,size_z,size_c);
    }

    //! Construct image with specified size and initialize pixel values from a memory buffer \overloading.
    CImg<T>& assign(const T *const values, const unsigned int size_x, const unsigned int size_y,
                    const unsigned int size_z, const unsigned int size_c, const bool is_shared) {
      const size_t siz = safe_size(size_x,size_y,size_z,size_c);
      if (!values || !siz) return assign();
      if (!is_shared) { if (_is_shared) assign(); assign(values,size_x,size_y,size_z,size_c); }
      else {
        if (!_is_shared) {
          if (values + siz<_data || values>=_data + size()) assign();
          else cimg::warn(_cimg_instance
                          "assign(): Shared image instance has overlapping memory.",

View on GitHub (pinned to f788b534b4)

Solutions

  1. Make the image pixel type match the buffer type, e.g. declare CImg<unsigned char> img; then img.assign(buf,w,h,d,c,true).
  2. Copy the data into a correctly-typed buffer before assigning (shared requires identical types).
  3. Pass is_shared=false if you want CImg to allocate and convert a copy of the buffer instead of sharing.

Example fix

// before
CImg<float> img;
unsigned char* buf = decodeBitmap();
img.assign(buf, w, h, 1, 3, true); // throws: pixel types differ
// after
CImg<unsigned char> img;
img.assign(buf, w, h, 1, 3, true); // types now match, sharing OK
Defensive patterns

Strategy: validation

Validate before calling

template<typename T, typename t>
bool canShare(const t* /*buf*/) {
  return strcmp(CImg<T>::pixel_type(), CImg<t>::pixel_type()) == 0;
}
// call: if (!canShare<T>(buf)) img.assign(buf,w,h,d,c,false); // copy instead

Type guard

template<typename T, typename t>
constexpr bool samePixelType = std::is_same_v<T, t>;

Prevention

When it happens

Trigger: Calling img.assign(buffer,w,h,d,s,true) (or the CImg constructor) where buffer is e.g. unsigned char* but img is CImg<float>, i.e. CImg<T>::pixel_type() != CImg<t>::pixel_type().

Common situations: Wrapping native bitmap/JPEG decode buffers (typically 8-bit unsigned char) into an image typed as float/double; copying shared-image code between pixel-type templates; recent change of image element type.

Related errors


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