{"record":{"id":"16eb408bc4479bf1","repo":"Yalantis/uCrop","slug":"cimg-instance-max-empty-instance","errorCode":null,"errorMessage":"_cimg_instance \"max(): Empty instance.\"","messagePattern":"_cimg_instance \"max\\(\\): Empty instance\\.\"","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":31807,"sourceCode":"      if (is_empty())\n        throw CImgInstanceException(_cimg_instance\n                                    \"minabs(): Empty instance.\",\n                                    cimg_instance);\n      const T *ptr_minabs = _data;\n      T minabs_value = *ptr_minabs;\n      cimg_for(*this,ptrs,T) {\n        const T ma = cimg::abs(*ptrs);\n        if (ma<minabs_value) { minabs_value = ma; ptr_minabs = ptrs; }\n      }\n      return *ptr_minabs;\n    }\n\n    //! Return a reference to the maximum pixel value.\n    /**\n     **/\n    T& max() {\n      if (is_empty())\n        throw CImgInstanceException(_cimg_instance\n                                    \"max(): Empty instance.\",\n                                    cimg_instance);\n      T *ptr_max = _data;\n      T max_value = *ptr_max;\n      cimg_for(*this,ptrs,T) if (*ptrs>max_value) max_value = *(ptr_max=ptrs);\n      return *ptr_max;\n    }\n\n    //! Return a reference to the maximum pixel value \\const.\n    const T& max() const {\n      if (is_empty())\n        throw CImgInstanceException(_cimg_instance\n                                    \"max(): Empty instance.\",\n                                    cimg_instance);\n      const T *ptr_max = _data;\n      T max_value = *ptr_max;\n      cimg_for(*this,ptrs,T) if (*ptrs>max_value) max_value = *(ptr_max=ptrs);\n      return *ptr_max;","sourceCodeStart":31789,"sourceCodeEnd":31825,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L31789-L31825","documentation":"Instance guard in CImg<T>::max(): the method must return a reference to the largest pixel, but the image is empty (no pixels allocated), so no maximum exists to reference. It is the standard empty-instance check shared by the min/max family.","triggerScenarios":"Calling img.max() on a default-constructed CImg or an image that became empty after assignment/crop/failed load.","commonSituations":"Computing normalization ranges on images that failed to load; optional overlay images never initialized; ROI cropping with empty intersection.","solutions":["Check img.is_empty() (or img.size()>0) before calling max()","Fix the load/crop logic that yielded an empty image"],"exampleFix":"// before\nfloat v = img.max();\n// after\nif (!img.is_empty()) { float v = img.max(); } else { /* handle empty */ }","handlingStrategy":"validation","validationCode":"if (!img.is_empty()) { ... img.max() ... }","typeGuard":null,"tryCatchPattern":"try { T v = img.max(); } catch (const CImgInstanceException& e) { /* handle empty image */ }","preventionTips":["Check load return state and is_empty() before normalization code","Avoid default-constructed images flowing into stats without guards"],"tags":["cimg","empty-instance","image-processing"],"backgroundTag":"empty-result-set","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}