[libav-api] memory leak

Reuben Sant reuben at imedia.com.mt
Fri Nov 22 10:32:03 CET 2013


I tried :

av_free(pkt.data), 
av_free_packet(&pkt);

The second free does not execute. The Microsoft c++ compiler indicates a 'corruption of the heap.

I think I'm freeing the same memory twice.

Reuben







-----Original Message-----
From: libav-api [mailto:libav-api-bounces at libav.org] On Behalf Of Michael Kannard
Sent: Il-Ħamis, 21 ta' Novembru 2013 19:12
To: libav-api at libav.org
Subject: Re: [libav-api] memory leak

When you call av_init_packet(..)
This does not initialize the data memory for the packet.
Therefore it is created when you call avcodec_encode_video2(.., dstFrame, got_pkt).
If the memory is initialized when you call that function, then you are responsible for destroying the memory for the data field.
av_free_packet(..) does not destroy that memory, so I think you would have to add a call to av_free(pkt->data), before the av_free_packet().

________________________________________
From: libav-api [libav-api-bounces at libav.org] on behalf of libav-api-request at libav.org [libav-api-request at libav.org]
Sent: Thursday, November 21, 2013 3:00 AM
To: libav-api at libav.org
Subject: libav-api Digest, Vol 33, Issue 7

Send libav-api mailing list submissions to
        libav-api at libav.org

To subscribe or unsubscribe via the World Wide Web, visit
        https://lists.libav.org/mailman/listinfo/libav-api
or, via email, send a message with subject or body 'help' to
        libav-api-request at libav.org

You can reach the person managing the list at
        libav-api-owner at libav.org

When replying, please edit your Subject line so it is more specific than "Re: Contents of libav-api digest..."


Today's Topics:

   1. memory leak (Reuben Sant)


----------------------------------------------------------------------

Message: 1
Date: Thu, 21 Nov 2013 10:04:46 +0000
From: Reuben Sant <reuben at imedia.com.mt>
To: "libav-api at libav.org" <libav-api at libav.org>
Subject: [libav-api] memory leak
Message-ID:
        <4D0C0179982BE04F9396736E68A6AEFA02288A5516 at hercules.imedia.com.mt>
Content-Type: text/plain; charset="us-ascii"

Hello,

I have a simple module in our application that encodes a generated raw image and writes the encoded packets to file. The allocated image size is 1920x1080.

Calls to avcodec_encode_video2  are allocating around 500MB of memory which is never freed up. I cannot find the source of this leak.

Below I have described the procedure I'm using. Some notes:


1.       if avcodec_encode_video2  does not return a got_pkt, I'm not freeing the packet because it was never allocated. Is that correct?


2.  Am I correctly freeing frames with:  av_freep and avcodec_free_frame ?


3.       Does sws_scale simply takes the srcFrame and copies the scaled/converted image to the already allocated dstFrame, or is there any malloc involved internally? If so, I'm not freeing anything after this call.


Thanks and Regards

Reuben





FUNCTION Start

av_register_all()
avformat_alloc_context(..)
avcodec_find_encoder
avformat_new_stream(..)
avcodec_open2(..)
srcFrame = avcodec_alloc_frame(..)
dstFrame = avcodec_alloc_frame(..)
av_image_alloc(srcFrame, ...)
av_image_alloc(dstFrame, ...)
sws_getContext(..)
avio_open(..)
avformat_write_header(..)

End FUNCTION




FUNCTION WriteLoop

while (..)
{
  fill srcFrame
  av_init_packet(..)
  sws_scale(srcFrame, dstFrame, ...)
  avcodec_encode_video2(.., dstFrame, got_pkt)
  if (got_pkt) WritePacket()
}

End FUNCTION




FUNCTION WritePacket

av_interleaved_write_frame(..)
av_free_packet(..)

End FUNCTION




FUNCTION Stop

got_pkt = 1

while (got_pkt)
{
  avcodec_encode_video2(.., NULL, got_pkt)
  if (got_pkt) WritePacket()
}

av_write_trailer(..)

av_freep(&srcFrame->data[0]);
avcodec_free_frame(&srcFrame);

av_freep(&dstFrame->data[0]);
avcodec_free_frame(&dstFrame);

//free the streams.
av_freep(streams[i]->codec);
av_freep(streams[i]);

avio_close(..);

av_free(avFormatContext);

sws_freeContext(..);


End FUNCTION


------------------------------

Subject: Digest Footer

_______________________________________________
libav-api mailing list
libav-api at libav.org
https://lists.libav.org/mailman/listinfo/libav-api

------------------------------

End of libav-api Digest, Vol 33, Issue 7
****************************************
This e-mail and any attachments are intended only for use by the addressee(s) named herein and may contain confidential information. If you are not the intended recipient of this e-mail, you are hereby notified any dissemination, distribution or copying of this email and any attachments is strictly prohibited. If you receive this email in error, please immediately notify the sender by return email and permanently delete the original, any copy and any printout thereof. The integrity and security of e-mail cannot be guaranteed.


_______________________________________________
libav-api mailing list
libav-api at libav.org
https://lists.libav.org/mailman/listinfo/libav-api


More information about the libav-api mailing list