[Libav-user] AVUtil memory leaks
Tomislav Timic
tomislavtimic98 at gmail.com
Mon May 12 12:48:32 EEST 2025
This is how I am opening output
AVFormatContext *fmtCtx = nullptr;
if (avformat_alloc_output_context2(&fmtCtx, nullptr,
output.format.c_str(), output.url.c_str()) < 0)
{
LOG(INFO) << "Failed to open mux format context";
release();
return false;
}
if (muxFormatCtx.at(0)->oformat->flags & AVFMT_GLOBALHEADER)
{
muxFormatCtx.at(0)->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
if (!(muxFormatCtx.at(0)->oformat->flags & AVFMT_NOFILE))
{
if (avio_open(&muxFormatCtx.at(0)->pb,
pipeline->getOutputs().outputs[0].url.c_str(),
AVIO_FLAG_WRITE) < 0)
{
LOG(INFO) << util::string::StringFmtUtil::format("Could not
open output format = %s",
pipeline->getOutputs().outputs[0].url.c_str());
release();
return false;
}
}
if (avformat_write_header(muxFormatCtx.at(0), nullptr) < 0)
{
LOG(ERROR) << "Error occurred while opening mux format";
release();
return false;
}
And this is how i free it
for (auto formatCtx: muxFormatCtx)
{
if (formatCtx != nullptr)
{
av_write_trailer(formatCtx);
if (!(formatCtx->oformat->flags & AVFMT_NOFILE))
{
avio_closep(&formatCtx->pb);
}
avformat_free_context(formatCtx);
formatCtx = nullptr;
}
}
muxFormatCtx.clear();
On Mon, May 12, 2025 at 11:20 AM Jaka Bac <jakabac at gmail.com> wrote:
> On Mon, 12 May 2025 at 11:02, Tomislav Timic <tomislavtimic98 at gmail.com>
> wrote:
>
>> I was observing my application that is doing transcoding of mpegts over
>> udp and I noticed that constantly after each create of transcoder pipeline
>> and delete of pipeline i get small leaks from avutil.
>> [image: Screenshot 2025-05-12 at 11.00.07.png]
>> This is how i release input format context
>>
>> if (formatCtx != nullptr)
>> {
>> avformat_close_input(&formatCtx);
>> avformat_free_context(formatCtx);
>> formatCtx = nullptr;
>> }
>>
>> This is how i release decoder
>>
>> if (frame != nullptr)
>> {
>> av_frame_free(&frame);
>> frame = nullptr;
>> }
>>
>> if (videoCodecCtx != nullptr)
>> {
>> if (videoCodecCtx->codec != nullptr &&
>> videoCodecCtx->codec->capabilities & AV_CODEC_CAP_ENCODER_FLUSH)
>> {
>> avcodec_flush_buffers(videoCodecCtx);
>> }
>> avcodec_free_context(&videoCodecCtx);
>> videoCodecCtx = nullptr;
>> }
>>
>> if (audioCodecCtx != nullptr)
>> {
>> if (audioCodecCtx->codec != nullptr &&
>> audioCodecCtx->codec->capabilities & AV_CODEC_CAP_ENCODER_FLUSH)
>> {
>> avcodec_flush_buffers(audioCodecCtx);
>> }
>> avcodec_free_context(&audioCodecCtx);
>> audioCodecCtx = nullptr;
>> }
>>
>> and this is how i release encoder and muxer
>>
>> if (codecCtx != nullptr)
>> {
>> avcodec_free_context(&codecCtx);
>> codecCtx = nullptr;
>> }
>>
>> if (encodePkt != nullptr)
>> {
>> av_packet_free(&encodePkt);
>> encodePkt = nullptr;
>> }
>>
>> if (packet != nullptr)
>> {
>> av_packet_free(&packet);
>> packet = nullptr;
>> }
>>
>> for (auto formatCtx: muxFormatCtx)
>> {
>> if (formatCtx != nullptr)
>> {
>> av_write_trailer(formatCtx);
>>
>> if (!(formatCtx->oformat->flags & AVFMT_NOFILE))
>> {
>> avio_closep(&formatCtx->pb);
>> }
>>
>> avformat_free_context(formatCtx);
>> formatCtx = nullptr;
>> }
>> }
>>
>>
> There is no need to do both avformat_close_input() and then
> avformat_free_context() since avformat_close_input will also free the
> context.
> avformat_free_context() in your case finds out that it got a null pointer
> and doesn't do anything.
>
> As for your leak, how are you opening the output? Maybe you are missing an
> avio_closep() ?
> _______________________________________________
> Libav-user mailing list
> Libav-user at ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/libav-user
>
> To unsubscribe, visit link above, or email
> libav-user-request at ffmpeg.org with subject "unsubscribe".
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20250512/65b20ccc/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screenshot 2025-05-12 at 11.00.07.png
Type: image/png
Size: 691804 bytes
Desc: not available
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20250512/65b20ccc/attachment.png>
More information about the Libav-user
mailing list