Tag Archives: Ffmpeg memory release

Ffmpeg about Avio_alloc_Context application for memory release

Question:

Using ffmpeg to discover AV_ The memory requested by malloc can’t use AV in the end_ If the free function is released, it will crash.

Code example:

   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;
    }
    /*.........*/

Release Avio_ alloc_ Context memory

Method 1: use Avio_ context_ free

            avio_context_free(&avio );

Method 2: using AV_ freep

            // memory release
            av_freep(&avio ->buffer);
            av_freep(&avio);

Appendix:

/**
 * 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);