{"record":{"id":"b263effd11163acd","repo":"Yalantis/uCrop","slug":"insert-invalid-insertion-request-of-specified-s","errorCode":null,"errorMessage":"insert(): Invalid insertion request of specified shared image CImg<%s>(%u,%u,%u,%u,%p) at position %u (pixel types are different).","messagePattern":"insert\\(\\): Invalid insertion request of specified shared image CImg<(.+?)>\\(%u,%u,%u,%u,%p\\) at position %u \\(pixel types are different\\)\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":65760,"sourceCode":"    //---------------------------\n\n    //! Insert a copy of the image \\c img into the current image list, at position \\c pos.\n    /**\n        \\param img Image to insert a copy to the list.\n        \\param pos Index of the insertion.\n        \\param is_shared Tells if the inserted image is a shared copy of \\c img or not.\n    **/\n    template<typename t>\n    CImgList<T>& insert(const CImg<t>& img, const unsigned int pos=~0U, const bool is_shared=false) {\n      const unsigned int npos = pos==~0U?_width:pos;\n      if (npos>_width)\n        throw CImgArgumentException(_cimglist_instance\n                                    \"insert(): Invalid insertion request of specified image (%u,%u,%u,%u,%p) \"\n                                    \"at position %u.\",\n                                    cimglist_instance,\n                                    img._width,img._height,img._depth,img._spectrum,img._data,npos);\n      if (is_shared)\n        throw CImgArgumentException(_cimglist_instance\n                                    \"insert(): Invalid insertion request of specified shared image \"\n                                    \"CImg<%s>(%u,%u,%u,%u,%p) at position %u (pixel types are different).\",\n                                    cimglist_instance,\n                                    img.pixel_type(),img._width,img._height,img._depth,img._spectrum,img._data,npos);\n\n      CImg<T> *const new_data = (++_width>_allocated_width)?new CImg<T>[_allocated_width?(_allocated_width<<=1):\n                                                                        (_allocated_width=16)]:0;\n      if (!_data) { // Insert new element into empty list\n        _data = new_data;\n        *_data = img;\n      } else {\n        if (new_data) { // Insert with re-allocation\n          if (npos) std::memcpy((void*)new_data,(void*)_data,sizeof(CImg<T>)*npos);\n          if (npos!=_width - 1)\n            std::memcpy((void*)(new_data + npos + 1),(void*)(_data + npos),sizeof(CImg<T>)*(_width - 1 - npos));\n          std::memset((void*)_data,0,sizeof(CImg<T>)*(_width - 1));\n          delete[] _data;\n          _data = new_data;","sourceCodeStart":65742,"sourceCodeEnd":65778,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L65742-L65778","documentation":"CImgList::insert() rejects inserting a shared CImg whose pixel type differs from the list's element type T. A shared image must share its buffer with the list, so mixing pixel types (e.g. inserting a shared CImg<unsigned char> into a CImgList<float>) is invalid. The library throws CImgArgumentException naming both the image dimensions and the mismatched pixel_type().","triggerScenarios":"Calling list.insert(img, pos, true) (is_shared=true) where img.pixel_type() != typeid of the list's T, e.g. inserting a shared unsigned-char image into a CImgList<float>.","commonSituations":"Mixing 8-bit images loaded from disk with float processing lists; reusing a shared-image wrapper from another list of a different template instantiation; template type changed in refactoring so the list element type no longer matches the shared image.","solutions":["Convert the image to the list's pixel type before inserting, or insert a non-shared copy (is_shared=false)","Change the CImgList template parameter so it matches the shared image's pixel type","Deep-copy via CImg<T>(img) to retype the data before sharing"],"exampleFix":"// before\nCImgList<float> list;\nCImg<unsigned char> img(\"in.png\");\nlist.insert(img, 0, true); // throws: pixel types are different\n// after\nlist.insert(img.get_resize(...).get_shared(), 0, true); // still invalid; correct:\nlist.insert(CImg<float>(img), 0, false); // or load as float\nlist.insert(CImg<float>(img), 0, true); // shared float image","handlingStrategy":"validation","validationCode":"if (img.is_shared() && img.pixel_type() != typeid(T).name()) { /* retype or copy */ }","typeGuard":"template<typename S, typename T> bool pixel_type_matches(const CImg<S>& img) { return std::is_same<S,T>::value; }","tryCatchPattern":"try { list.insert(img, pos, true); } catch (const CImgArgumentException& e) { list.insert(CImg<T>(img), pos, false); }","preventionTips":["Never pass shared=true for images of a different pixel type","Keep lists and images in the same template instantiation per pipeline stage","Deep-copy when retyping instead of sharing"],"tags":["cimg","pixel-type-mismatch","shared-image","argument-validation"],"backgroundTag":"type-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"}