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

Wenpeng Zhou zhou.wenpeng at rdnet.fi
Tue Oct 30 12:43:28 CET 2012


Hi Hendrik

Thanks!

-----Original Message-----
From: libav-user-bounces at ffmpeg.org [mailto:libav-user-bounces at ffmpeg.org]
On Behalf Of Hendrik Leppkes
Sent: 30. lokakuuta 2012 13:34
To: This list is about using libavcodec, libavformat, libavutil, libavdevice
and libavfilter.
Subject: Re: [Libav-user] (AVStream *)av_malloc(sizeof(AVStream)) crashed
the application

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
_______________________________________________
Libav-user mailing list
Libav-user at ffmpeg.org
http://ffmpeg.org/mailman/listinfo/libav-user



More information about the Libav-user mailing list