Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to new & delete AVPacket?

I'm working with a FFmpeg project right now, I have to store the data of AVPacket which from av_read_frame, and fill data to new AVPacket for following decoding.

Here is my problem: when I try to new & free an AVPacket, memory leaks always happen.

I am just doing a simple testing:

for(;;) {
    AVPacket pkt;
    av_new_packet(&pkt, 1000);
    av_init_packet(&pkt);
    av_free_packet(&pkt);
}

What am I doing wrong?

like image 712
PatrickSCLin Avatar asked Aug 22 '14 02:08

PatrickSCLin


1 Answers

  • av_new_packet creates a packet and allocates data
  • av_init_packet sets all packet members to default, and sets data pointer to NULL, the leak is here
  • av_free_packet clears all visible members, but your data is already leaking

If you want FFmpeg to allocate the data for you, do not call av_init_packet. If you want to handle the data yourself, allocate the packet object on the stack and set its data yourself (and free it yourself):

AVPacket pkt;
av_init_packet(&pkt);
pkt.data = dataBuffer;
pkt.size = dataBufferSize;
// use your packet
// free your dataBuffer
like image 192
biskitt Avatar answered Sep 28 '22 18:09

biskitt