Yalantis/uCrop · error · CImgInstanceException
trace(): Empty instance.
Error message
trace(): Empty instance.
What it means
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.
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.
Example fix
// before CImg<float> m; // default constructed, empty double t = m.trace(); // after CImg<float> m(3,3,1,1, data3x3); if (!m.is_empty()) double t = m.trace();
Defensive patterns
Strategy: validation
Validate before calling
if (img.is_empty()) { /* handle: load failed or unassigned */ } else double t = img.trace(); Type guard
bool usableMatrix = !img.is_empty();
Try / catch
try { double t = img.trace(); } catch (CImgInstanceException& e) { std::cerr << e.what() << '\n'; } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.
Related errors
- atXYZC(): Empty instance.
- [" cimg_appname "_math_parser] CImg<
- [cimg_appname_math_parser] CImg<
- [cimg_appname_math_parser] CImg<
- [cimg_appname_math_parser] CImg<
AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08).
Data as JSON: /api/errors/5ef7fa90aa297136.
Report an issue: GitHub.
Appendix: source
Thrown at ucrop/src/main/jni/CImg.h:32566
cimg_pragma_openmp(parallel for reduction(+:res) cimg_openmp_if_size(size(),8192))
for (longT off = 0; off<(longT)siz; ++off) res+=(double)(_data[off]?1:0);
} else if (cimg::type<float>::is_inf(magnitude_type)) { // L-inf
cimg_for(*this,ptrs,T) { const double val = (double)cimg::abs(*ptrs); if (val>res) res = val; }
} else { // L-p
cimg_pragma_openmp(parallel for reduction(+:res) cimg_openmp_if_size(size(),8192))
for (longT off = 0; off<(longT)siz; ++off)
res+=(double)std::pow((double)cimg::abs(_data[off]),(double)magnitude_type);
res = (double)std::pow(res,1.0/magnitude_type);
}
return res;
}
//! Compute the trace of the image, viewed as a matrix.
/**
**/
double trace() const {
if (is_empty())
throw CImgInstanceException(_cimg_instance
"trace(): Empty instance.",
cimg_instance);
double res = 0;
cimg_forX(*this,k) res+=(double)(*this)(k,k);
return res;
}
//! Compute the determinant of the image, viewed as a matrix.
/**
**/
double det() const {
if (is_empty() || _width!=_height || _depth!=1 || _spectrum!=1)
throw CImgInstanceException(_cimg_instance
"det(): Instance is not a square matrix.",
cimg_instance);
switch (_width) {
case 1 : return (double)((*this)(0,0));View on GitHub (pinned to f788b534b4)