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?
av_new_packet
creates a packet and allocates dataav_init_packet
sets all packet members to default, and sets data pointer to NULL
,
the leak is hereav_free_packet
clears all visible members, but
your data is already leakingIf 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With