<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)">}<br></span></pre></div></div></div>