{"record":{"id":"ab4ddbb8c03ab1e4","repo":"Yalantis/uCrop","slug":"cimglist-s-fft-failed-to-allocate-memory-s-ab4ddb","errorCode":null,"errorMessage":"CImgList<%s>::FFT(): Failed to allocate memory (%s) for computing FFT of image (%u,%u,%u,%u).","messagePattern":"CImgList<(.+?)>::FFT\\(\\): Failed to allocate memory \\((.+?)\\) for computing FFT of image \\(%u,%u,%u,%u\\)\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"critical","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":47947,"sourceCode":"      if (!real)\n        throw CImgInstanceException(\"CImgList<%s>::FFT(): Empty specified real part.\",\n                                    pixel_type());\n      if (!imag) imag.assign(real._width,real._height,real._depth,real._spectrum,(T)0);\n      if (!real.is_sameXYZC(imag))\n        throw CImgInstanceException(\"CImgList<%s>::FFT(): Specified real part (%u,%u,%u,%u,%p) and \"\n                                    \"imaginary part (%u,%u,%u,%u,%p) have different dimensions.\",\n                                    pixel_type(),\n                                    real._width,real._height,real._depth,real._spectrum,real._data,\n                                    imag._width,imag._height,imag._depth,imag._spectrum,imag._data);\n      cimg::unused(nb_threads);\n#ifdef cimg_use_fftw3\n      cimg::mutex(12);\n#ifndef cimg_use_fftw3_singlethread\n      fftw_plan_with_nthreads(nb_threads?nb_threads:cimg::nb_cpus());\n#endif\n      fftw_complex *data_in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*real._width*real._height*real._depth);\n      if (!data_in)\n        throw CImgInstanceException(\"CImgList<%s>::FFT(): Failed to allocate memory (%s) \"\n                                    \"for computing FFT of image (%u,%u,%u,%u).\",\n                                    pixel_type(),\n                                    cimg::strbuffersize(sizeof(fftw_complex)*real._width*\n                                                        real._height*real._depth*real._spectrum),\n                                    real._width,real._height,real._depth,real._spectrum);\n      double *const ptrf = (double*)data_in;\n      fftw_plan data_plan =\n        real._depth>1?fftw_plan_dft_3d(real._depth,real._height,real._width,data_in,data_in,\n                                       is_inverse?FFTW_BACKWARD:FFTW_FORWARD,FFTW_ESTIMATE):\n        real._height>1?fftw_plan_dft_2d(real._height,real._width,data_in,data_in,\n                                        is_inverse?FFTW_BACKWARD:FFTW_FORWARD,FFTW_ESTIMATE):\n        fftw_plan_dft_1d(real._width,data_in,data_in,\n                         is_inverse?FFTW_BACKWARD:FFTW_FORWARD,FFTW_ESTIMATE);\n      cimg_forC(real,c) {\n        CImg<T> realc = real.get_shared_channel(c), imagc = imag.get_shared_channel(c);\n        cimg_pragma_openmp(parallel for cimg_openmp_if_size(real.width()*real.height()*real.depth(),125000))\n        cimg_rofoff(realc,i) { const ulongT i2 = 2*i; ptrf[i2] = (double)realc[i]; ptrf[i2 + 1] = (double)imagc[i]; }\n        fftw_execute(data_plan);","sourceCodeStart":47929,"sourceCodeEnd":47965,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L47929-L47965","documentation":"Like error 291 but on the generic FFT path: with FFTW3 enabled, CImg::FFT allocates fftw_complex storage for the whole width*height*depth volume. If fftw_malloc returns NULL the allocation size (via cimg::strbuffersize) and image dimensions are reported in this CImgInstanceException. It means the process ran out of memory for the temporary FFT buffer.","triggerScenarios":"Calling CImg<T>::FFT(real, imag) or CImgList::FFT() compiled with cimg_use_fftw3 on a volume where width*height*depth*16 bytes exceeds available heap.","commonSituations":"Large 3D volumes or huge 2D images (e.g. gigapixel scans), 32-bit builds limited to ~2-3 GB, containers with tight memory limits, or heavy fragmentation from prior large allocations.","solutions":["Reduce image/volume size (downsample, crop, tile) before FFT.","Raise the memory limit (64-bit build, larger container/cgroup limit, more RAM).","Process in tiles/slabs along one axis and stitch results.","Free cached images/other buffers before the FFT to reduce peak usage."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"size_t bytes = sizeof(fftw_complex) * (size_t)img.width()*img.height()*img.depth();\nif (bytes > memBudget()) { /* tile or downsample first */ }","typeGuard":null,"tryCatchPattern":"try { img.FFT(real, imag); }\ncatch (CImgInstanceException& e) { std::cerr << \"Out of memory for FFT (\" << e.what() << \")\"; /* fallback: tiled FFT */ }","preventionTips":["Compute 16*W*H*D byte cost before FFT and compare against free RAM","Use 64-bit builds for large volumes","Release large intermediate images before FFT to lower peak memory","Implement tiled FFT for volumes above a size threshold"],"tags":["cimg","fft","memory","allocation"],"backgroundTag":"memory-allocation-failed","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}