{"record":{"id":"41199de2ce7f7ae1","repo":"Yalantis/uCrop","slug":"assign-invalid-assignment-request-of-shared-ins-41199d","errorCode":null,"errorMessage":"assign(): Invalid assignment request of shared instance from (%s*) buffer (pixel types are different).","messagePattern":"assign\\(\\): Invalid assignment request of shared instance from \\((.+?)\\*\\) buffer \\(pixel types are different\\)\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":13708,"sourceCode":"          _width = _height = _depth = _spectrum = 0; _data = 0;\n          throw CImgInstanceException(_cimg_instance\n                                      \"assign(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).\",\n                                      cimg_instance,\n                                      cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c),\n                                      size_x,size_y,size_z,size_c);\n        }\n        std::memcpy((void*)new_data,(void*)values,siz*sizeof(T));\n        delete[] _data; _data = new_data; _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c;\n      }\n      return *this;\n    }\n\n    //! Construct image with specified size and initialize pixel values from a memory buffer \\overloading.\n    template<typename t>\n    CImg<T>& assign(const t *const values, const unsigned int size_x, const unsigned int size_y,\n                    const unsigned int size_z, const unsigned int size_c, const bool is_shared) {\n      if (is_shared)\n        throw CImgArgumentException(_cimg_instance\n                                    \"assign(): Invalid assignment request of shared instance from (%s*) buffer \"\n                                    \"(pixel types are different).\",\n                                    cimg_instance,\n                                    CImg<t>::pixel_type());\n      return assign(values,size_x,size_y,size_z,size_c);\n    }\n\n    //! Construct image with specified size and initialize pixel values from a memory buffer \\overloading.\n    CImg<T>& assign(const T *const values, const unsigned int size_x, const unsigned int size_y,\n                    const unsigned int size_z, const unsigned int size_c, const bool is_shared) {\n      const size_t siz = safe_size(size_x,size_y,size_z,size_c);\n      if (!values || !siz) return assign();\n      if (!is_shared) { if (_is_shared) assign(); assign(values,size_x,size_y,size_z,size_c); }\n      else {\n        if (!_is_shared) {\n          if (values + siz<_data || values>=_data + size()) assign();\n          else cimg::warn(_cimg_instance\n                          \"assign(): Shared image instance has overlapping memory.\",","sourceCodeStart":13690,"sourceCodeEnd":13726,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L13690-L13726","documentation":"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.","triggerScenarios":"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().","commonSituations":"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.","solutions":["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).","Copy the data into a correctly-typed buffer before assigning (shared requires identical types).","Pass is_shared=false if you want CImg to allocate and convert a copy of the buffer instead of sharing."],"exampleFix":"// before\nCImg<float> img;\nunsigned char* buf = decodeBitmap();\nimg.assign(buf, w, h, 1, 3, true); // throws: pixel types differ\n// after\nCImg<unsigned char> img;\nimg.assign(buf, w, h, 1, 3, true); // types now match, sharing OK","handlingStrategy":"validation","validationCode":"template<typename T, typename t>\nbool canShare(const t* /*buf*/) {\n  return strcmp(CImg<T>::pixel_type(), CImg<t>::pixel_type()) == 0;\n}\n// call: if (!canShare<T>(buf)) img.assign(buf,w,h,d,c,false); // copy instead","typeGuard":"template<typename T, typename t>\nconstexpr bool samePixelType = std::is_same_v<T, t>;","tryCatchPattern":null,"preventionTips":["Let the buffer type drive the CImg template parameter (CImg<unsigned char> for 8-bit buffers).","Only pass is_shared=true when you own the buffer and types match exactly.","Prefer is_shared=false unless wrapping large buffers where copying is too costly."],"tags":["cimg","pixel-type","shared-instance","type-mismatch"],"backgroundTag":"dtype-mismatch","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}