Module_demuxer: memory pool from CMD_DEMUXER_INIT_MEM_POOL is never freed (memory leak on every mm_module_close)

SDK: ameba-rtos-pro2, release 9.6_r (AmebaPro2 / RTL8735B)
File: component/media/mmfv2/module_demuxer.c

Summary
The memory pool allocated by CMD_DEMUXER_INIT_MEM_POOL (memory_init(mem_total_size, mem_block_size)) is never released. Neither CMD_DEMUXER_CLOSE nor demuxer_destroy() calls memory_deinit(ctx->mem_pool). So every demuxer instance that is opened and closed leaks mem_total_size bytes of heap.

How to reproduce

  1. Open a demuxer: mm_module_open(&demuxer_module).
  2. Send CMD_DEMUXER_SET_PARAMS (e.g. mem_total_size = 1 MB), MM_CMD_SET_QUEUE_LEN, MM_CMD_INIT_QUEUE_ITEMS, CMD_DEMUXER_INIT_MEM_POOL, CMD_DEMUXER_OPEN, CMD_DEMUXER_STREAM_START.
  3. Play the file, then send CMD_DEMUXER_CLOSE and call mm_module_close().
  4. Repeat for the next file, e.g. when playing several MP4 files from the SD card one after another.

Observed
Free heap (xPortGetFreeHeapSizeExt(0)) drops by about mem_total_size (≈1.05–1.09 MB with a 1 MB pool) for every file played, and is never recovered. After ~50 files the DDR heap is exhausted.

Expected
The pool should be freed when the module is destroyed. module_aac.c already does this for its own pool (memory_deinit(ctx->mem_pool)).

Suggested fix (tested on hardware)
In demuxer_destroy(), free the pool after the worker tasks have been deleted:

void *demuxer_destroy(void *p)
{
demuxer_ctx_t *ctx = (demuxer_ctx_t *)p;

  if (ctx)        {
          if (ctx->audio_handler) {
                  vTaskDelete(ctx->audio_handler);
                  ctx->audio_handler = NULL;
          }
          if (ctx->video_handler) {
                  vTaskDelete(ctx->video_handler);
                  ctx->video_handler = NULL;
          }
          if (ctx->demuxer_sema) {
                  vSemaphoreDelete(ctx->demuxer_sema);
          }
          if (ctx->mem_pool) {                 /* added */
                  memory_deinit(ctx->mem_pool);    /* added */
                  ctx->mem_pool = NULL;            /* added */
          }
          if (ctx->mp4_demuxer_ctx) {
                  free(ctx->mp4_demuxer_ctx);
          }
          free(ctx);
  }
  return NULL;

}

This ordering looks safe: in libmmf.a, mm_module_close() first drains the module’s queues and calls del_item (which runs memory_free() on the pool) for every item, and only then calls destroy. With this change, heap stayed flat across 14 chained playback files (previously ~15 MB lost).

Related observations in the same module

  • CMD_DEMUXER_STOP is declared in module_demuxer.h but not handled in demuxer_control() (it has no effect).
  • CMD_DEMUXER_GET_STATUS is declared but not implemented, so applications have to read demuxer_v_state from the context to detect end of file.
  • CMD_DEMUXER_STREAM_START always creates new worker tasks, and the worker tasks never exit on their own, so a demuxer instance can’t be restarted for a new file. Each file needs a new instance, which is what makes the pool leak above show up.

Hi @deepakkumarbinwal ,

Thank you for the summary. Please find the following patch for critical memory leaks issue. As for the further implementation of CMD_DEMUXER will be fixed in the near future. Thank you.

demuxer_patch_0910.zip (120.3 KB)