DrKLO/Telegram · error

Memory allocation failure!\n

Error message

Memory allocation failure!\n

What it means

Thrown by a YUV-to-JPEG conversion utility when malloc() fails to allocate yuv_buffer for the raw YUV frame data. The allocation size (yuv_size) is the full 4:2:0 planar frame: luma_width*luma_height + 2*chroma_width*chroma_height. This is a fatal, non-recoverable path — the function closes the YUV file handle and returns 1 immediately.

Source

Thrown at TMessagesProj/jni/mozjpeg/yuvjpeg.c:171

    return 1;
  }

  fseek(yuv_fd, 0, SEEK_END);
  yuv_size = ftell(yuv_fd);
  fseek(yuv_fd, 0, SEEK_SET);

  /* Check that the file size matches 4:2:0 yuv. */
  if (yuv_size !=
   (size_t)luma_width*luma_height + 2*chroma_width*chroma_height) {
    fclose(yuv_fd);
    fprintf(stderr, "Unexpected input format!\n");
    return 1;
  }

  yuv_buffer = malloc(yuv_size);
  if (!yuv_buffer) {
    fclose(yuv_fd);
    fprintf(stderr, "Memory allocation failure!\n");
    return 1;
  }

  if (fread(yuv_buffer, yuv_size, 1, yuv_fd) != 1) {
    fprintf(stderr, "Error reading yuv file\n");
  };

  fclose(yuv_fd);

  frame_width = (luma_width + (16 - 1)) & ~(16 - 1);
  frame_height = (luma_height + (16 - 1)) & ~(16 - 1);

  jpg_buffer =
   malloc(frame_width*frame_height + 2*(frame_width/2)*(frame_height/2));
  if (!jpg_buffer) {
    free(yuv_buffer);
    fprintf(stderr, "Memory allocation failure!\n");
    return 1;

View on GitHub (pinned to 45ab8f4308)

Solutions

  1. Verify the YUV source reports sane dimensions before calling the converter; reject or downscale frames whose computed yuv_size exceeds an available-memory budget.
  2. Reduce the input resolution or process in tiled/striped chunks instead of one whole-frame allocation.
  3. Confirm the device has enough free heap; on Android check ActivityManager.getMemoryInfo() before invoking the native path.
  4. If building for 32-bit, switch to a 64-bit ABI so size_t and the virtual address space can handle large frames.

Example fix

// before
yuv_buffer = malloc(yuv_size);
if (!yuv_buffer) {
    fclose(yuv_fd);
    fprintf(stderr, "Memory allocation failure!\n");
    return 1;
}

// after: bound the allocation against available memory before trying
if (yuv_size > MAX_SAFE_YUV_BYTES || yuv_size > get_free_heap_bytes()) {
    fclose(yuv_fd);
    fprintf(stderr, "YUV frame too large to allocate (%zu bytes)\n", yuv_size);
    return 1;
}
yuv_buffer = malloc(yuv_size);
if (!yuv_buffer) {
    fclose(yuv_fd);
    fprintf(stderr, "Memory allocation failure!\n");
    return 1;
}
Defensive patterns

Strategy: validation

Validate before calling

// Validate the computed YUV size against available heap before malloc.
size_t need = (size_t)luma_width*luma_height + 2*(size_t)chroma_width*chroma_height;
long free_bytes = get_free_heap_bytes(); /* platform-specific, e.g. mallinfo/ActivityManager */
if (need > MAX_SAFE_YUV_BYTES || (free_bytes > 0 && (size_t)free_bytes < need)) {
    fclose(yuv_fd);
    fprintf(stderr, "YUV frame too large (%zu bytes, %ld free)\n", need, free_bytes);
    return 1;
}

Prevention

When it happens

Trigger: malloc(yuv_size) returns NULL. Occurs when the parsed YUV dimensions produce an extremely large yuv_size, when the process is already near its memory limit, or on 32-bit builds where size_t cannot represent huge frames (though the prior size check passes, the heap is still exhausted).

Common situations: Processing very high-resolution video frames (e.g. 4K/8K YUV) on a memory-constrained Android device; running under the Telegram JNI memory ceiling; a malformed YUV header reporting inflated dimensions; device under memory pressure from other apps.

Related errors


AI-assisted analysis of DrKLO/Telegram@45ab8f4308 (2026-08-14). Data as JSON: /api/errors/6874d1987a7819a7. Report an issue: GitHub.