{"record":{"id":"b5a75f50ab54f544","repo":"mudler/LocalAI","slug":"failed-to-allocate-memory-for-resized-image-n","errorCode":null,"errorMessage":"Failed to allocate memory for resized image\\n","messagePattern":"Failed to allocate memory for resized image\\\\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"backend/go/stablediffusion-ggml/cpp/gosd.cpp","lineNumber":856,"sourceCode":"        int img_height = 0;\n        input_image_buffer = stbi_load(src_image, &img_width, &img_height, &c, 3);\n        if (input_image_buffer == NULL) {\n            fprintf(stderr, \"Failed to load input image from '%s'\\n\", src_image);\n            return 1;\n        }\n        if (c < 3) {\n            fprintf(stderr, \"Input image must have at least 3 channels, got %d\\n\", c);\n            free(input_image_buffer);\n            return 1;\n        }\n\n        // Resize input image if dimensions don't match\n        if (img_width != width || img_height != height) {\n            fprintf(stderr, \"Resizing input image from %dx%d to %dx%d\\n\", img_width, img_height, width, height);\n\n            uint8_t* resized_image_buffer = (uint8_t*)malloc(height * width * 3);\n            if (resized_image_buffer == NULL) {\n                fprintf(stderr, \"Failed to allocate memory for resized image\\n\");\n                free(input_image_buffer);\n                return 1;\n            }\n\n            stbir_resize(input_image_buffer, img_width, img_height, 0,\n                         resized_image_buffer, width, height, 0, STBIR_TYPE_UINT8,\n                         3, STBIR_ALPHA_CHANNEL_NONE, 0,\n                         STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP,\n                         STBIR_FILTER_BOX, STBIR_FILTER_BOX,\n                         STBIR_COLORSPACE_SRGB, nullptr);\n\n            free(input_image_buffer);\n            input_image_buffer = resized_image_buffer;\n        }\n\n        p->init_image = {(uint32_t)width, (uint32_t)height, 3, input_image_buffer};\n        p->strength = strength;\n        fprintf(stderr, \"Using img2img with strength: %.2f\\n\", strength);","sourceCodeStart":838,"sourceCodeEnd":874,"githubUrl":"https://github.com/mudler/LocalAI/blob/44413a9d06bf5bc52ce088ba8ca74e5a2e8bee26/backend/go/stablediffusion-ggml/cpp/gosd.cpp#L838-L874","documentation":"Hard error in the img2img resize step: malloc(height * width * 3) for the destination buffer returned NULL because the requested width x height is too large for available memory. The input buffer is freed and generation aborts with code 1. It is purely an allocation-size problem, not a model problem.","triggerScenarios":"Requesting output dimensions far larger than the input image (e.g. 4096x4096 upscale in img2img) on a memory-constrained host or inside a container with a low memory limit; integer-overflow-sized dimensions could also yield absurd allocations.","commonSituations":"See trigger scenarios.","solutions":["Reduce width/height in the request to a value the process can afford (bytes needed = width*height*3 plus model memory)","Raise the container/process memory limit or free other loaded models first","Sanity-cap dimensions at the API layer so clients cannot request absurd sizes"],"exampleFix":"// before\nwidth = 8192; height = 8192;  // 8192*8192*3 = 192MB buffer, may fail\n\n// after\nwidth = 2048; height = 2048;  // ~12.6MB buffer","handlingStrategy":"validation","validationCode":"// Go caller: cap dimensions by affordable allocation size\nconst maxPixels = 2048 * 2048\nif req.Width <= 0 || req.Height <= 0 || req.Width*req.Height > maxPixels {\n    return fmt.Errorf(\"dimensions %dx%d exceed limit %d megapixels\", req.Width, req.Height, maxPixels/1_000_000)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Enforce a max-resolution cap at the API layer sized to the host's free memory (bytes needed = w*h*3 plus model weights)","Set container memory limits with headroom for resize buffers, not just model weights"],"tags":["stablediffusion","img2img","memory","allocation"],"backgroundTag":null,"analyzedSha":"44413a9d06bf5bc52ce088ba8ca74e5a2e8bee26","analyzedAt":"2026-08-15T10:13:50.291Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}