{"record":{"id":"15fa510ef8f8cdf9","repo":"Yalantis/uCrop","slug":"load-tiff-failed-to-allocate-memory-s-for-fi","errorCode":null,"errorMessage":"load_tiff(): Failed to allocate memory (%s) for file '%s'.","messagePattern":"load_tiff\\(\\): Failed to allocate memory \\((.+?)\\) for file '(.+?)'\\.","errorType":"exception","errorClass":"CImgException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":58194,"sourceCode":"        voxel_size[1] = 1.f/voxel_size[1];\n      }\n      if (description) {\n        const char *s_description = 0;\n        if (TIFFGetField(tif,TIFFTAG_IMAGEDESCRIPTION,&s_description) && s_description)\n          CImg<charT>::string(s_description).move_to(*description);\n      }\n      const unsigned int spectrum = !is_spp || photo>=3?(photo>1?3:1):samplesperpixel;\n      assign(nx,ny,1,spectrum);\n\n      if ((photo>=3 && sampleformat==1 &&\n           (bitspersample==4 || bitspersample==8) &&\n           (samplesperpixel==1 || samplesperpixel==3 || samplesperpixel==4)) ||\n          (bitspersample==1 && samplesperpixel==1)) {\n        // Special case for unsigned color images.\n        cimg_uint32 *const raster = (cimg_uint32*)_TIFFmalloc(nx*ny*sizeof(cimg_uint32));\n        if (!raster) {\n          _TIFFfree(raster); TIFFClose(tif);\n          throw CImgException(_cimg_instance\n                              \"load_tiff(): Failed to allocate memory (%s) for file '%s'.\",\n                              cimg_instance,\n                              cimg::strbuffersize(nx*ny*sizeof(cimg_uint32)),filename);\n        }\n        TIFFReadRGBAImage(tif,nx,ny,raster,0);\n        switch (spectrum) {\n        case 1 :\n          cimg_forXY(*this,x,y)\n            (*this)(x,y,0) = (T)(float)TIFFGetR(raster[nx*(ny - 1 -y) + x]);\n          break;\n        case 3 :\n          cimg_forXY(*this,x,y) {\n            (*this)(x,y,0) = (T)(float)TIFFGetR(raster[nx*(ny - 1 - y) + x]);\n            (*this)(x,y,1) = (T)(float)TIFFGetG(raster[nx*(ny - 1 - y) + x]);\n            (*this)(x,y,2) = (T)(float)TIFFGetB(raster[nx*(ny - 1 - y) + x]);\n          }\n          break;\n        case 4 :","sourceCodeStart":58176,"sourceCodeEnd":58212,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L58176-L58212","documentation":"In the RGBA fast path, load_tiff() allocates a raster buffer of nx*ny*sizeof(cimg_uint32) bytes with _TIFFmalloc. If that allocation returns null, the library throws a CImgException naming the requested buffer size, meaning the system could not supply that much memory.","triggerScenarios":"Loading a very large TIFF (huge width*height) such that nx*ny*4 bytes exceeds available memory, e.g. a multi-gigapixel scan on a memory-constrained Android device.","commonSituations":"Mobile/JNI environments with low per-process memory limits, 32-bit address-space exhaustion, or 32-bit overflow of nx*ny*sizeof() for gigantic dimensions.","solutions":["Free memory or reduce concurrent allocations before loading.","Downscale or crop the image before load (e.g. preprocess with tiffcp -Z or vips).","Load the image on a device/process with a larger memory budget (64-bit).","Use the tile/strip read path instead of the RGBA path by changing the file's storage layout."],"exampleFix":"// before: 40000x40000 RGBA => ~6.4 GB raster\nimg.load_tiff(\"huge_scan.tif\");\n// after: downscale first\n// $ tiffcp -Z 8000 huge_scan.tif small.tif\nimg.load_tiff(\"small.tif\");","handlingStrategy":"validation","validationCode":"long long need = (long long)width * height * 4;\nif (need > availableMemoryBudget()) throw std::runtime_error(\"TIFF too large for memory budget\");","typeGuard":null,"tryCatchPattern":"try { img.load_tiff(path); }\ncatch (CImgException &e) { if (strstr(e.what(), \"allocate memory\")) downscaleThenReload(path); else throw; }","preventionTips":["Estimate nx*ny*4 and compare to device memory before loading","Downscale huge scans before load","Avoid 32-bit processes for very large images","Watch for integer overflow in size computations"],"tags":["cimg","tiff","out-of-memory"],"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"}