[Libav-user] (AVStream *)av_malloc(sizeof(AVStream)) crashed the application

Hendrik Leppkes h.leppkes at gmail.com
Tue Oct 30 12:34:22 CET 2012


On Tue, Oct 30, 2012 at 12:28 PM, Chandranath Bhattacharyya
<cbhattac at adobe.com> wrote:
>>> Hi Chandranath
>
>
>
>>> Thanks!
>
>
>
>>> But I already allocated memory for the pointer *ic_enc_mjpeg
>
>                 OK. Sorry. I did not realize you meant the crash occurred
> even after allocation.
>
>
>
>>> AVFormatContext *ic_enc_mjpeg;
>
>>> ic_enc_mjpeg = (AVFormatContext *)av_malloc(sizeof(AVFormatContext));
>
>
>
>>> Then I tried to allocate the memory for ic_enc_mjpeg->streams[0], then it
>>> crashed.
>
>
>
> streams variable inside AVFormatContext is AVStream **. So the allocation of
> AVFormatContext using av_malloc is not supposed to initialize/allocate it.
> Hence the crash.
>
>
>
> Check the documentation in header avformat.h for more info.
>
>
>
> The crash can be fixed by the following code.
>
>
>
> AVFormatContext *ic_enc_mjpeg;
>
> ic_enc_mjpeg = (AVFormatContext *)av_malloc(sizeof(AVFormatContext));
>
> ic_enc_mjpeg->streams = (AVStream **)av_malloc(sizeof(AVStream *)); // ç DO
> THIS.
>
> ic_enc_mjpeg->nb_streams = 1; // OR WHATEVER.
>
> ic_enc_mjpeg->streams[0] = (AVStream *)av_malloc(sizeof(AVStream));
>
>
>


Hi,
you should not manually allocate such structures, use the appropriate
functions for this.

AVFormatContext *ic_enc_mjpeg;
ic_enc_mjpeg = avformat_alloc_context();
AVStream *st = avformat_new_stream(ic_enc_mjpeg, NULL);

.. and then do your work with the "st" object.

Ideally, also pass an AVCodec object to avformat_new_stream instead of
NULL, appropriate for the codec that you want to use.

- Hendrik


More information about the Libav-user mailing list