{"record":{"id":"1828a51e966905d5","repo":"Yalantis/uCrop","slug":"cimg-invalid-construction-request-of-a-u-u","errorCode":null,"errorMessage":"CImg(): Invalid construction request of a (%u,%u,%u,%u) shared instance from a (%s*) buffer (pixel types are different).","messagePattern":"CImg\\(\\): Invalid construction request of a \\(%u,%u,%u,%u\\) shared instance from a \\((.+?)\\*\\) buffer \\(pixel types are different\\)\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":13267,"sourceCode":"       - A \\c CImgInstanceException is thrown when the pixel buffer cannot be allocated\n         (e.g. when requested size is too big for available memory).\n       \\warning\n       - You must take care when operating on a shared image, since it may have an invalid pixel buffer pointer data()\n         (e.g. already deallocated).\n       \\par Example\n       \\code\n       unsigned char tab[256*256] = {};\n       CImg<unsigned char> img1(tab,256,256,1,1,false), // Construct new non-shared image from buffer 'tab'\n                           img2(tab,256,256,1,1,true); // Construct new shared-image from buffer 'tab'\n       tab[1024] = 255; // Here, 'img2' is indirectly modified, but not 'img1'\n       \\endcode\n    **/\n    template<typename t>\n    CImg(const t *const values, const unsigned int size_x, const unsigned int size_y=1,\n         const unsigned int size_z=1, const unsigned int size_c=1, const bool is_shared=false):_is_shared(false) {\n      if (is_shared) {\n        _width = _height = _depth = _spectrum = 0; _data = 0;\n        throw CImgArgumentException(_cimg_instance\n                                    \"CImg(): Invalid construction request of a (%u,%u,%u,%u) shared instance \"\n                                    \"from a (%s*) buffer (pixel types are different).\",\n                                    cimg_instance,\n                                    size_x,size_y,size_z,size_c,CImg<t>::pixel_type());\n      }\n      const size_t siz = safe_size(size_x,size_y,size_z,size_c);\n      if (values && siz) {\n        _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c;\n        try { _data = new T[siz]; } catch (...) {\n          _width = _height = _depth = _spectrum = 0; _data = 0;\n          throw CImgInstanceException(_cimg_instance\n                                      \"CImg(): 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        }\n        const t *ptrs = values; cimg_for(*this,ptrd,T) *ptrd = (T)*(ptrs++);","sourceCodeStart":13249,"sourceCodeEnd":13285,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L13249-L13285","documentation":"The CImg(const t* values, ...) constructor refuses (CImgArgumentException) when is_shared=true and the buffer's pixel type t differs from the image's type T. A shared instance aliases foreign memory without copying, which is only possible when the pixel types match exactly; the library rejects the mismatched shared request up front.","triggerScenarios":"CImg<float>( (unsigned char*)buf, w, h, 1, 1, true ) — constructing a shared image from a buffer of a different element type, e.g. passing a raw uint8 buffer to a float image with shared=true.","commonSituations":"Wrapping camera/OpenCV buffers of type uint8 into float CImg shared instances; template code where T and t silently differ; casting away the actual buffer type to force the constructor.","solutions":["Match the types: use CImg<unsigned char> for a uint8 buffer (shared) or CImg<float> for a float buffer.","Drop is_shared=true so the constructor copies/converts the buffer instead of aliasing it.","Explicitly convert the buffer to the target pixel type before constructing a shared view."],"exampleFix":"// before\nunsigned char buf[640*480];\nCImg<float> img(buf, 640, 480, 1, 1, true); // throws: type mismatch\n// after\nCImg<unsigned char> img(buf, 640, 480, 1, 1, true); // matching shared type\n// or copy: CImg<float> img(buf, 640, 480, 1, 1, false);","handlingStrategy":"type-guard","validationCode":"if (is_shared && !std::is_same<T,t>::value) return fail_shared_type_mismatch();\nCImg<T> img(buf, w, h, z, c, is_shared);","typeGuard":"template<typename T, typename t>\nbool shared_types_ok(bool is_shared) { return !is_shared || std::is_same<T,t>::value; }","tryCatchPattern":"try { CImg<float> img(buf, w, h, 1, 1, true); }\ncatch (cimg_library::CImgArgumentException &e) { fprintf(stderr, \"%s\", e.what()); }","preventionTips":["Use static_assert(std::is_same<T,t>::value) in template wrappers around shared construction.","Only set is_shared=true when you own/know the buffer's exact type.","Let non-shared construction copy+convert when types differ."],"tags":["cimg","type-mismatch","shared-instance"],"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"}