How to new & delete AVPacket?

  • 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