<div dir="ltr"><div dir="ltr">On Mon, 12 May 2025 at 11:02, Tomislav Timic <<a href="mailto:tomislavtimic98@gmail.com">tomislavtimic98@gmail.com</a>> wrote:</div><div class="gmail_quote gmail_quote_container"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>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. <br><img src="cid:ii_makur3vy0" alt="Screenshot 2025-05-12 at 11.00.07.png" width="558" height="311"><br></div><div>This is how i release input format context <br></div><div><br></div><div><div style="background-color:rgb(33,33,33);color:rgb(238,255,255)"><pre style="font-family:Menlo,monospace;font-size:9pt"><span style="color:rgb(199,146,234);font-style:italic">if </span><span style="color:rgb(137,221,255)">(</span>formatCtx <span style="color:rgb(137,221,255)">!= </span><span style="color:rgb(199,146,234);font-style:italic">nullptr</span><span style="color:rgb(137,221,255)">)<br></span><span style="color:rgb(137,221,255)">{<br></span><span style="color:rgb(137,221,255)">    </span><span style="color:rgb(130,170,255)">avformat_close_input</span><span style="color:rgb(137,221,255)">(&</span>formatCtx<span style="color:rgb(137,221,255)">);<br></span><span style="color:rgb(137,221,255)">    </span><span style="color:rgb(130,170,255)">avformat_free_context</span><span style="color:rgb(137,221,255)">(</span>formatCtx<span style="color:rgb(137,221,255)">);<br></span><span style="color:rgb(137,221,255)">    </span>formatCtx <span style="color:rgb(137,221,255)">= </span><span style="color:rgb(199,146,234);font-style:italic">nullptr</span><span style="color:rgb(137,221,255)">;<br></span><span style="color:rgb(137,221,255)">}</span></pre></div></div><div>This is how i release decoder <br></div><div><div style="background-color:rgb(33,33,33);color:rgb(238,255,255)"><pre style="font-family:Menlo,monospace;font-size:9pt"><span style="color:rgb(199,146,234);font-style:italic">if </span><span style="color:rgb(137,221,255)">(</span>frame <span style="color:rgb(137,221,255)">!= </span><span style="color:rgb(199,146,234);font-style:italic">nullptr</span><span style="color:rgb(137,221,255)">)<br></span><span style="color:rgb(137,221,255)">{<br></span><span style="color:rgb(137,221,255)">    </span><span style="color:rgb(130,170,255)">av_frame_free</span><span style="color:rgb(137,221,255)">(&</span>frame<span style="color:rgb(137,221,255)">);<br></span><span style="color:rgb(137,221,255)">    </span>frame <span style="color:rgb(137,221,255)">= </span><span style="color:rgb(199,146,234);font-style:italic">nullptr</span><span style="color:rgb(137,221,255)">;<br></span><span style="color:rgb(137,221,255)">}<br></span><span style="color:rgb(137,221,255)"><br></span><span style="color:rgb(199,146,234);font-style:italic">if </span><span style="color:rgb(137,221,255)">(</span>videoCodecCtx <span style="color:rgb(137,221,255)">!= </span><span style="color:rgb(199,146,234);font-style:italic">nullptr</span><span style="color:rgb(137,221,255)">)<br></span><span style="color:rgb(137,221,255)">{<br></span><span style="color:rgb(137,221,255)">    </span><span style="color:rgb(199,146,234);font-style:italic">if </span><span style="color:rgb(137,221,255)">(</span>videoCodecCtx<span style="color:rgb(137,221,255)">-></span>codec <span style="color:rgb(137,221,255)">!= </span><span style="color:rgb(199,146,234);font-style:italic">nullptr </span><span style="color:rgb(137,221,255)">&&<br></span><span style="color:rgb(137,221,255)">        </span>videoCodecCtx<span style="color:rgb(137,221,255)">-></span>codec<span style="color:rgb(137,221,255)">-></span>capabilities <span style="color:rgb(137,221,255)">& </span><span style="color:rgb(255,203,107);font-weight:bold">AV_CODEC_CAP_ENCODER_FLUSH</span><span style="color:rgb(137,221,255)">)<br></span><span style="color:rgb(137,221,255)">    {<br></span><span style="color:rgb(137,221,255)">        </span><span style="color:rgb(130,170,255)">avcodec_flush_buffers</span><span style="color:rgb(137,221,255)">(</span>videoCodecCtx<span style="color:rgb(137,221,255)">);<br></span><span style="color:rgb(137,221,255)">    }<br></span><span style="color:rgb(137,221,255)">    </span><span style="color:rgb(130,170,255)">avcodec_free_context</span><span style="color:rgb(137,221,255)">(&</span>videoCodecCtx<span style="color:rgb(137,221,255)">);<br></span><span style="color:rgb(137,221,255)">    </span>videoCodecCtx <span style="color:rgb(137,221,255)">= </span><span style="color:rgb(199,146,234);font-style:italic">nullptr</span><span style="color:rgb(137,221,255)">;<br></span><span style="color:rgb(137,221,255)">}<br></span><span style="color:rgb(137,221,255)"><br></span><span style="color:rgb(199,146,234);font-style:italic">if </span><span style="color:rgb(137,221,255)">(</span>audioCodecCtx <span style="color:rgb(137,221,255)">!= </span><span style="color:rgb(199,146,234);font-style:italic">nullptr</span><span style="color:rgb(137,221,255)">)<br></span><span style="color:rgb(137,221,255)">{<br></span><span style="color:rgb(137,221,255)">    </span><span style="color:rgb(199,146,234);font-style:italic">if </span><span style="color:rgb(137,221,255)">(</span>audioCodecCtx<span style="color:rgb(137,221,255)">-></span>codec <span style="color:rgb(137,221,255)">!= </span><span style="color:rgb(199,146,234);font-style:italic">nullptr </span><span style="color:rgb(137,221,255)">&&<br></span><span style="color:rgb(137,221,255)">        </span>audioCodecCtx<span style="color:rgb(137,221,255)">-></span>codec<span style="color:rgb(137,221,255)">-></span>capabilities <span style="color:rgb(137,221,255)">& </span><span style="color:rgb(255,203,107);font-weight:bold">AV_CODEC_CAP_ENCODER_FLUSH</span><span style="color:rgb(137,221,255)">)<br></span><span style="color:rgb(137,221,255)">    {<br></span><span style="color:rgb(137,221,255)">        </span><span style="color:rgb(130,170,255)">avcodec_flush_buffers</span><span style="color:rgb(137,221,255)">(</span>audioCodecCtx<span style="color:rgb(137,221,255)">);<br></span><span style="color:rgb(137,221,255)">    }<br></span><span style="color:rgb(137,221,255)">    </span><span style="color:rgb(130,170,255)">avcodec_free_context</span><span style="color:rgb(137,221,255)">(&</span>audioCodecCtx<span style="color:rgb(137,221,255)">);<br></span><span style="color:rgb(137,221,255)">    </span>audioCodecCtx <span style="color:rgb(137,221,255)">= </span><span style="color:rgb(199,146,234);font-style:italic">nullptr</span><span style="color:rgb(137,221,255)">;<br></span><span style="color:rgb(137,221,255)">}</span></pre></div>and this is how i release encoder and muxer <br></div><div><br></div><div><div style="background-color:rgb(33,33,33);color:rgb(238,255,255)"><pre style="font-family:Menlo,monospace;font-size:9pt"><span style="color:rgb(199,146,234);font-style:italic">if </span><span style="color:rgb(137,221,255)">(</span>codecCtx <span style="color:rgb(137,221,255)">!= </span><span style="color:rgb(199,146,234);font-style:italic">nullptr</span><span style="color:rgb(137,221,255)">)<br></span><span style="color:rgb(137,221,255)">{<br></span><span style="color:rgb(137,221,255)">    </span><span style="color:rgb(130,170,255)">avcodec_free_context</span><span style="color:rgb(137,221,255)">(&</span>codecCtx<span style="color:rgb(137,221,255)">);<br></span><span style="color:rgb(137,221,255)">    </span>codecCtx <span style="color:rgb(137,221,255)">= </span><span style="color:rgb(199,146,234);font-style:italic">nullptr</span><span style="color:rgb(137,221,255)">;<br></span><span style="color:rgb(137,221,255)">}<br></span><span style="color:rgb(137,221,255)"><br></span><span style="color:rgb(199,146,234);font-style:italic">if </span><span style="color:rgb(137,221,255)">(</span>encodePkt <span style="color:rgb(137,221,255)">!= </span><span style="color:rgb(199,146,234);font-style:italic">nullptr</span><span style="color:rgb(137,221,255)">)<br></span><span style="color:rgb(137,221,255)">{<br></span><span style="color:rgb(137,221,255)">    </span><span style="color:rgb(130,170,255)">av_packet_free</span><span style="color:rgb(137,221,255)">(&</span>encodePkt<span style="color:rgb(137,221,255)">);<br></span><span style="color:rgb(137,221,255)">    </span>encodePkt <span style="color:rgb(137,221,255)">= </span><span style="color:rgb(199,146,234);font-style:italic">nullptr</span><span style="color:rgb(137,221,255)">;<br></span><span style="color:rgb(137,221,255)">}<br><br></span></pre></div><div style="background-color:rgb(33,33,33);color:rgb(238,255,255)"><pre style="font-family:Menlo,monospace;font-size:9pt"><span style="color:rgb(199,146,234);font-style:italic">if </span><span style="color:rgb(137,221,255)">(</span>packet <span style="color:rgb(137,221,255)">!= </span><span style="color:rgb(199,146,234);font-style:italic">nullptr</span><span style="color:rgb(137,221,255)">)<br></span><span style="color:rgb(137,221,255)">{<br></span><span style="color:rgb(137,221,255)">    </span><span style="color:rgb(130,170,255)">av_packet_free</span><span style="color:rgb(137,221,255)">(&</span>packet<span style="color:rgb(137,221,255)">);<br></span><span style="color:rgb(137,221,255)">    </span>packet <span style="color:rgb(137,221,255)">= </span><span style="color:rgb(199,146,234);font-style:italic">nullptr</span><span style="color:rgb(137,221,255)">;<br></span><span style="color:rgb(137,221,255)">}<br></span><span style="color:rgb(137,221,255)"><br></span><span style="color:rgb(199,146,234);font-style:italic">for </span><span style="color:rgb(137,221,255)">(</span><span style="color:rgb(199,146,234);font-style:italic">auto </span>formatCtx<span style="color:rgb(137,221,255)">: </span>muxFormatCtx<span style="color:rgb(137,221,255)">)<br></span><span style="color:rgb(137,221,255)">{<br></span><span style="color:rgb(137,221,255)">    </span><span style="color:rgb(199,146,234);font-style:italic">if </span><span style="color:rgb(137,221,255)">(</span>formatCtx <span style="color:rgb(137,221,255)">!= </span><span style="color:rgb(199,146,234);font-style:italic">nullptr</span><span style="color:rgb(137,221,255)">)<br></span><span style="color:rgb(137,221,255)">    {<br></span><span style="color:rgb(137,221,255)">        </span><span style="color:rgb(130,170,255)">av_write_trailer</span><span style="color:rgb(137,221,255)">(</span>formatCtx<span style="color:rgb(137,221,255)">);<br></span><span style="color:rgb(137,221,255)"><br></span><span style="color:rgb(137,221,255)">        </span><span style="color:rgb(199,146,234);font-style:italic">if </span><span style="color:rgb(137,221,255)">(!(</span>formatCtx<span style="color:rgb(137,221,255)">-></span>oformat<span style="color:rgb(137,221,255)">-></span>flags <span style="color:rgb(137,221,255)">& </span><span style="color:rgb(255,203,107);font-weight:bold">AVFMT_NOFILE</span><span style="color:rgb(137,221,255)">))<br></span><span style="color:rgb(137,221,255)">        {<br></span><span style="color:rgb(137,221,255)">            </span><span style="color:rgb(130,170,255)">avio_closep</span><span style="color:rgb(137,221,255)">(&</span>formatCtx<span style="color:rgb(137,221,255)">-></span>pb<span style="color:rgb(137,221,255)">);<br></span><span style="color:rgb(137,221,255)">        }<br></span><span style="color:rgb(137,221,255)"><br></span><span style="color:rgb(137,221,255)">        </span><span style="color:rgb(130,170,255)">avformat_free_context</span><span style="color:rgb(137,221,255)">(</span>formatCtx<span style="color:rgb(137,221,255)">);<br></span><span style="color:rgb(137,221,255)">        </span>formatCtx <span style="color:rgb(137,221,255)">= </span><span style="color:rgb(199,146,234);font-style:italic">nullptr</span><span style="color:rgb(137,221,255)">;<br></span><span style="color:rgb(137,221,255)">    }<br></span><span style="color:rgb(137,221,255)">}</span></pre></div></div></div></blockquote><div><br></div><div>There is no need to do both avformat_close_input() and then avformat_free_context() since avformat_close_input will also free the context.</div><div>avformat_free_context() in your case finds out that it got a null pointer and doesn't do anything.</div><div><br></div><div>As for your leak, how are you opening the output? Maybe you are missing an avio_closep() ?</div></div></div>