Yalantis/uCrop · error · CImgArgumentException
CImg(): Invalid construction request of a (%u,%u,%u,%u) shar
Error message
CImg(): Invalid construction request of a (%u,%u,%u,%u) shared instance from a (%s*) buffer (pixel types are different).
What it means
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.
Source
Thrown at ucrop/src/main/jni/CImg.h:13267
- A \c CImgInstanceException is thrown when the pixel buffer cannot be allocated
(e.g. when requested size is too big for available memory).
\warning
- You must take care when operating on a shared image, since it may have an invalid pixel buffer pointer data()
(e.g. already deallocated).
\par Example
\code
unsigned char tab[256*256] = {};
CImg<unsigned char> img1(tab,256,256,1,1,false), // Construct new non-shared image from buffer 'tab'
img2(tab,256,256,1,1,true); // Construct new shared-image from buffer 'tab'
tab[1024] = 255; // Here, 'img2' is indirectly modified, but not 'img1'
\endcode
**/
template<typename t>
CImg(const t *const values, const unsigned int size_x, const unsigned int size_y=1,
const unsigned int size_z=1, const unsigned int size_c=1, const bool is_shared=false):_is_shared(false) {
if (is_shared) {
_width = _height = _depth = _spectrum = 0; _data = 0;
throw CImgArgumentException(_cimg_instance
"CImg(): Invalid construction request of a (%u,%u,%u,%u) shared instance "
"from a (%s*) buffer (pixel types are different).",
cimg_instance,
size_x,size_y,size_z,size_c,CImg<t>::pixel_type());
}
const size_t siz = safe_size(size_x,size_y,size_z,size_c);
if (values && siz) {
_width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c;
try { _data = new T[siz]; } catch (...) {
_width = _height = _depth = _spectrum = 0; _data = 0;
throw CImgInstanceException(_cimg_instance
"CImg(): 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);
}
const t *ptrs = values; cimg_for(*this,ptrd,T) *ptrd = (T)*(ptrs++);View on GitHub (pinned to f788b534b4)
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.
Example fix
// before unsigned char buf[640*480]; CImg<float> img(buf, 640, 480, 1, 1, true); // throws: type mismatch // after CImg<unsigned char> img(buf, 640, 480, 1, 1, true); // matching shared type // or copy: CImg<float> img(buf, 640, 480, 1, 1, false);
Defensive patterns
Strategy: type-guard
Validate before calling
if (is_shared && !std::is_same<T,t>::value) return fail_shared_type_mismatch(); CImg<T> img(buf, w, h, z, c, is_shared);
Type guard
template<typename T, typename t>
bool shared_types_ok(bool is_shared) { return !is_shared || std::is_same<T,t>::value; } Try / catch
try { CImg<float> img(buf, w, h, 1, 1, true); }
catch (cimg_library::CImgArgumentException &e) { fprintf(stderr, "%s", e.what()); } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- assign(): Invalid assignment request of shared instance from
- [cimg_appname_math_parser] CImg<%s>::%s: %s: Invalid types i
- [cimg_appname_math_parser] CImg<%s>::%s: %s: Invalid type '%
- [" cimg_appname "_math_parser] CImg<%s>::%s: %s%s %s%s has i
- cimg::SDL3_attr(): %s
AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08).
Data as JSON: /api/errors/1828a51e966905d5.
Report an issue: GitHub.