{"record":{"id":"5ef7fa90aa297136","repo":"Yalantis/uCrop","slug":"trace-empty-instance","errorCode":null,"errorMessage":"trace(): Empty instance.","messagePattern":"trace\\(\\): Empty instance\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":32566,"sourceCode":"        cimg_pragma_openmp(parallel for reduction(+:res) cimg_openmp_if_size(size(),8192))\n        for (longT off = 0; off<(longT)siz; ++off) res+=(double)(_data[off]?1:0);\n      } else if (cimg::type<float>::is_inf(magnitude_type)) { // L-inf\n        cimg_for(*this,ptrs,T) { const double val = (double)cimg::abs(*ptrs); if (val>res) res = val; }\n      } else { // L-p\n        cimg_pragma_openmp(parallel for reduction(+:res) cimg_openmp_if_size(size(),8192))\n        for (longT off = 0; off<(longT)siz; ++off)\n          res+=(double)std::pow((double)cimg::abs(_data[off]),(double)magnitude_type);\n        res = (double)std::pow(res,1.0/magnitude_type);\n      }\n      return res;\n    }\n\n    //! Compute the trace of the image, viewed as a matrix.\n    /**\n     **/\n    double trace() const {\n      if (is_empty())\n        throw CImgInstanceException(_cimg_instance\n                                    \"trace(): Empty instance.\",\n                                    cimg_instance);\n      double res = 0;\n      cimg_forX(*this,k) res+=(double)(*this)(k,k);\n      return res;\n    }\n\n    //! Compute the determinant of the image, viewed as a matrix.\n    /**\n     **/\n    double det() const {\n      if (is_empty() || _width!=_height || _depth!=1 || _spectrum!=1)\n        throw CImgInstanceException(_cimg_instance\n                                    \"det(): Instance is not a square matrix.\",\n                                    cimg_instance);\n\n      switch (_width) {\n      case 1 : return (double)((*this)(0,0));","sourceCodeStart":32548,"sourceCodeEnd":32584,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L32548-L32584","documentation":"CImg::trace() computes the trace of the image viewed as a square matrix (sum of diagonal elements (k,k)). It throws CImgInstanceException when the image instance is empty (no pixel data), because a trace is undefined for a zero-sized matrix.","triggerScenarios":"Calling img.trace() on a default-constructed CImg (CImg<T> img;), on an image after assign() with zero dimensions, or on a CImg that failed to load a file and remained empty.","commonSituations":"Loading an image file that does not exist or fails to decode, then doing matrix math on the result; building a matrix dynamically and forgetting the assign() step; using a moved-from CImg instance.","solutions":["Assign the image before calling trace(), e.g. img.assign(w,h,1,1) with real data or load from file.","Check img.is_empty() before calling trace().","If the source may be an unloaded file, verify the load succeeded (img.width()>0) before matrix operations."],"exampleFix":"// before\nCImg<float> m; // default constructed, empty\ndouble t = m.trace();\n// after\nCImg<float> m(3,3,1,1, data3x3);\nif (!m.is_empty()) double t = m.trace();","handlingStrategy":"validation","validationCode":"if (img.is_empty()) { /* handle: load failed or unassigned */ } else double t = img.trace();","typeGuard":"bool usableMatrix = !img.is_empty();","tryCatchPattern":"try { double t = img.trace(); } catch (CImgInstanceException& e) { std::cerr << e.what() << '\\n'; }","preventionTips":["Check is_empty() after any load() call before doing math","Never do matrix ops on default-constructed CImg objects","Assign matrices with explicit dimensions and data"],"tags":["cimg","matrix","empty-instance"],"backgroundTag":"empty-required-field","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"}