【三分钟学习FFMPEG一个知识点】FFMPEG关于avio_alloc_context申请使用内存释放问题

548 阅读1分钟

在这里插入图片描述

问题:

使用ffmpeg发现av_malloc申请的内存最后不能用av_free函数释放,会崩溃。

代码示例:

   unsigned char * iobuffer = NULL; 
    iobuffer = (unsigned char *)av_malloc(40000);
    if (iobuffer == NULL)
    {
        printf("iobuffer av_malloc failed.\n");
        return -1;
    }

    AVIOContext *avio = avio_alloc_context(iobuffer, 40000, 0, this, fill_iobuffer, NULL, NULL);
    if (avio == NULL)
    {
        printf(" avio_alloc_context failed.\n");
        return -1;
    }
    /*.........*/

释放avio_alloc_context的内存

方法1:使用avio_context_free

            avio_context_free(&avio );

方法2:使用av_freep

            // 释放内存
            av_freep(&avio ->buffer);
            av_freep(&avio);

附录:

/**
 * Free the supplied IO context and everything associated with it.
 *
 * @param s Double pointer to the IO context. This function will write NULL
 * into s.
 */
void avio_context_free(AVIOContext **s);



/**
 * Free a memory block which has been allocated with a function of av_malloc()
 * or av_realloc() family, and set the pointer pointing to it to `NULL`.
 *
 * @code{.c}
 * uint8_t *buf = av_malloc(16);
 * av_free(buf);
 * // buf now contains a dangling pointer to freed memory, and accidental
 * // dereference of buf will result in a use-after-free, which may be a
 * // security risk.
 *
 * uint8_t *buf = av_malloc(16);
 * av_freep(&buf);
 * // buf is now NULL, and accidental dereference will only result in a
 * // NULL-pointer dereference.
 * @endcode
 *
 * @param ptr Pointer to the pointer to the memory block which should be freed
 * @note `*ptr = NULL` is safe and leads to no action.
 * @see av_free()
 */
void av_freep(void *ptr);