Yalantis/uCrop · error · CImgArgumentException

[" cimg_appname "_math_parser] CImg<%s>: Function '%s()': Sp

Error message

[" cimg_appname "_math_parser] CImg<%s>: Function '%s()': Specified image #%u of size (%d,%d,%d,%d) cannot be used as dynamic array%s.

What it means

Dynamic-array MP functions treat a listed image as a dynamic array: it must be a single-column image (width==1, depth==1) whose last row element stores a valid element counter (0 <= siz <= height-1). When the image shape or its counter is invalid, this error is thrown, appending '(contains invalid element counter)' when the shape is fine but the counter is wrong.

Source

Thrown at ucrop/src/main/jni/CImg.h:26263

      }

      static double mp_cut(_cimg_math_parser& mp) {
        double val = _mp_arg(2), cmin = _mp_arg(3), cmax = _mp_arg(4);
        return cimg::cut(val,cmin,cmax);
      }

      static double mp_da_back_or_pop(_cimg_math_parser& mp) {
        const bool is_pop_heap = mp.opcode[4]==2, is_pop = (bool)mp.opcode[4];
        const char *const s_op = is_pop_heap?"da_pop_heap":is_pop?"da_pop":"da_back";
        mp_check_list(mp,s_op);
        const unsigned int
          dim = (unsigned int)mp.opcode[2],
          ind = (unsigned int)cimg::mod((int)_mp_arg(3),mp.imglist.width());
        double *const ptrd = &_mp_arg(1) + (dim>1?1:0);
        CImg<T> &img = mp.imglist[ind];
        int siz = img?(int)cimg::float2uint((float)img[img._height - 1]):0;
        if (img && (img._width!=1 || img._depth!=1 || siz<0 || siz>img.height() - 1))
          throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function '%s()': "
                                      "Specified image #%u of size (%d,%d,%d,%d) cannot be used as dynamic array%s.",
                                      mp.imgout.pixel_type(),s_op,ind,
                                      img.width(),img.height(),img.depth(),img.spectrum(),
                                      img._width==1 && img._depth==1?"":" (contains invalid element counter)");
        if (!siz)
          throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function '%s()': "
                                      "Specified dynamic array #%u contains no elements.",
                                      mp.imgout.pixel_type(),s_op,ind);
        const int siz1 = siz - 1;
        if (is_pop_heap) { // Heapify-down
          if (dim==1) cimg::swap(img[0],img[siz1]);
          else {
            T *ptr0 = img.data(), *ptr1 = img.data(0,siz1);
            cimg_forC(img,c) { cimg::swap(*ptr0,*ptr1); ptr0+=img._height; ptr1+=img._height; }
          }
          int index = 0;
          while (true) {
            const int child_left = 2*index + 1, child_right = child_left + 1;

View on GitHub (pinned to f788b534b4)

Solutions

  1. Initialize the dynamic array first, e.g. fill("da_init(h,dim)") to set proper shape and counter.
  2. Ensure the image is a single-column (width 1, depth 1) image before array operations.
  3. Reset the element counter stored in the last row to a value <= height-1.
  4. Re-create the array image instead of reusing arbitrary images.

Example fix

// before
CImg<float> arr(10,10,1,3,0); arr.fill("da_pop(0)");
// after
CImg<float> arr(1,11,1,3,0); arr.fill("da_init(11,3)"); arr.fill("da_pop(0)");
Defensive patterns

Strategy: validation

Validate before calling

bool validDynArray = arr.width()==1 && arr.depth()==1;
float counter = arr ? arr(arr.width()-1, arr.height()-1) : 0.f;
if (!validDynArray || counter < 0 || counter > arr.height()-1)
  throw std::runtime_error("image is not a valid dynamic array");

Type guard

bool isDynamicArray(const CImg<T>& a) {
  if (a.is_empty() || a.width()!=1 || a.depth()!=1) return false;
  double siz = a(a.width()-1, a.height()-1);
  return siz >= 0 && siz <= a.height()-1;
}

Try / catch

try { img.fill(expr); } catch (const cimg_library::CImgArgumentException& e) { /* re-init array with da_init */ }

Prevention

When it happens

Trigger: Calling heap functions like 'da_pop'/'minha'-style MP opcodes on an image that is not 1 x H x 1 x S, or whose last-row counter value exceeds height-1 or is negative.

Common situations: Using a normal 2D image as a dynamic array without initializing its counter; corrupted array images after bad resize; forgetting to create the array with da_init.

Understand the failure class

Background: Tensor shape mismatch errors ("must have shape", "expected shape ... got ..."): when tensor dimensions disagree with what an op or layer was told to expect — this error's family across 6 libraries.

Related errors


AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08). Data as JSON: /api/errors/158872fe87fc72b7. Report an issue: GitHub.