Yalantis/uCrop · critical · CImgInstanceException

CImgList<%s>::FFT(): Failed to allocate memory (%s) for comp

Error message

CImgList<%s>::FFT(): Failed to allocate memory (%s) for computing FFT of image (%u,%u,%u,%u) along the X-axis.

What it means

When CImg is built with FFTW3 (cimg_use_fftw3), FFT() allocates a temporary fftw_complex buffer of size width*height*depth via fftw_malloc. If that allocation fails (returns NULL), this CImgInstanceException is thrown naming the requested buffer size and image dimensions. It signals the process could not obtain the memory needed for the X-axis transform.

Source

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

                                    pixel_type(),
                                    real._width,real._height,real._depth,real._spectrum,real._data,
                                    imag._width,imag._height,imag._depth,imag._spectrum,imag._data);
      const char _axis = cimg::lowercase(axis);
      if (_axis!='x' && _axis!='y' && _axis!='z')
        throw CImgArgumentException("CImgList<%s>::FFT(): Invalid specified axis '%c' for real and imaginary parts "
                                    "(%u,%u,%u,%u) "
                                    "(should be { x | y | z }).",
                                    pixel_type(),axis,
                                    real._width,real._height,real._depth,real._spectrum);
      cimg::unused(nb_threads);
#ifdef cimg_use_fftw3
      cimg::mutex(12);
#ifndef cimg_use_fftw3_singlethread
      fftw_plan_with_nthreads(nb_threads?nb_threads:cimg::nb_cpus());
#endif
      fftw_complex *data_in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*real._width*real._height*real._depth);
      if (!data_in)
        throw CImgInstanceException("CImgList<%s>::FFT(): Failed to allocate memory (%s) "
                                    "for computing FFT of image (%u,%u,%u,%u) along the X-axis.",
                                    pixel_type(),
                                    cimg::strbuffersize(sizeof(fftw_complex)*real._width*real._height*real._depth),
                                    real._width,real._height,real._depth,real._spectrum);
      double *const ptrf = (double*)data_in;
      fftw_plan data_plan =
        _axis=='x'?fftw_plan_many_dft(1,(int*)&real._width,real.height()*real.depth(),
                                      data_in,0,1,real.width(),
                                      data_in,0,1,real.width(),
                                      is_inverse?FFTW_BACKWARD:FFTW_FORWARD,FFTW_ESTIMATE):
        _axis=='y'?fftw_plan_many_dft(1,(int*)&real._height,real.width()*real.depth(),
                                      data_in,0,1,real.height(),
                                      data_in,0,1,real.height(),
                                      is_inverse?FFTW_BACKWARD:FFTW_FORWARD,FFTW_ESTIMATE):
        fftw_plan_many_dft(1,(int*)&real._depth,real.width()*real.height(),
                           data_in,0,1,real.depth(),
                           data_in,0,1,real.depth(),
                           is_inverse?FFTW_BACKWARD:FFTW_FORWARD,FFTW_ESTIMATE);

View on GitHub (pinned to f788b534b4)

Solutions

  1. Reduce input image size (crop, downsample, or tile the volume) before FFT.
  2. Increase available memory: raise container/cgroup limits, move to a 64-bit build, or free other allocations.
  3. Process the FFT axis-by-axis on smaller sub-volumes instead of the whole image at once.
  4. Check for memory leaks elsewhere that progressively exhaust the heap.
Defensive patterns

Strategy: validation

Validate before calling

size_t bytes = sizeof(fftw_complex) * (size_t)img.width()*img.height()*img.depth();
if (bytes > availableMemoryBudget()) throw std::runtime_error("FFT buffer too large");

Try / catch

try { list.FFT(is_inverse); }
catch (CImgInstanceException& e) { std::cerr << "FFT alloc failed, reducing size: " << e.what(); /* fall back to smaller ROI */ }

Prevention

When it happens

Trigger: Calling CImgList<T>::FFT()/CImg<T>::FFT() on a 3D image whose width*height*depth*sizeof(fftw_complex) exceeds available memory, while compiled with cimg_use_fftw3, computing along the X-axis.

Common situations: Processing very large volumes (e.g. 2048^3 or bigger), 32-bit address-space exhaustion, memory fragmentation, or running inside a container with a low memory limit/cgroup cap.

Related errors


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