[Libav-user] avpicture_free crashing when using with sws_scale()

Stefano Sabatini stefasab at gmail.com
Thu Jun 27 12:23:53 CEST 2013

In data Wednesday 2013-06-26 03:25:55 -0700, Pradeep Karosiya ha scritto:
> Hi Stefano,
> Thanks for your explanation. However I didn't get about your point on 
> fill pic with data and destroying previously allocating data.
> I've buffer filled with image data and I want to create AVPicture object
> from it so that it can
> be encoded to video. The inpic is used as temporary storage for conversion
> with input pix_format is different than required pix_format. Thats why I
> used
> AVPicture inpic
>  avpicture_alloc(&inpic,(PixelFormat)input_pix_fmt, width, height); 
>  avpicture_fill(&inpic, (uint8_t*)buf, (PixelFormat)input_pix_fmt, 
>  width, height); 

You don't need avpicture_alloc() at all, since you're already
providing the buffer in avpicture_fill(). In other words
avpicture_fill() will overwrite the reference to the memory allocated
by avpicture_alloc().

Note: you can also use the equivalent av_image_alloc() and
av_image_fill_arrays() API which is better documented and a bit more

> First I'm allocating AVPicture then I'm filling it with data from buf. And
> finally after conversion 
> I'm destroying it. But you said that there will be leak on call of
> avpicture_fill which is correct 
> as I'm also seeing the leaking in my code.
> So what is the best way to do this without leak. 
> I've also tried following

> void WriteFrame(const uint8_t*buf)
> {
>     const uint8_t *indata[1] = {buf};
>     int inlinesize[1] = {3*width};     
>     if (sws_scale(img_convert_ctx, indata, inlinesize, 0,height,
> dst_picture.data, dst_picture.linesize) < 0 )
>    {
>        std::cout<<"Conversion Failed "<<std::endl;
>    } 

>    else
>   {
>      avpicture_fill(&dst_picture, (uint8_t*)buf, (PixelFormat)input_pix_fmt,
> width, height);
>    }

This looks pointless, since you already filled dst_picture with the
converted data.


More information about the Libav-user mailing list