<html>
<head>
</head>
<body class='hmmessage'><div dir='ltr'>

<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style>
<div dir="ltr">I am implementing a custom IO to read data packets then transcode them.<div>The problem I have is memory leakage, which I guess is coming from the custom IO buffer I allocate to read the data.</div><div>I am referring to the avio example. I have exactly the same callback function.</div><div><div class="line" style="font-stretch: normal; font-size: 13px; line-height: 1; font-family: monospace, fixed; min-height: 13px; white-space: pre-wrap; word-wrap: break-word; text-indent: -53px; padding-left: 53px; padding-bottom: 0px; margin: 0px; transition-property: background-color, box-shadow; transition-duration: 0.5s;"><span class="keyword" style="color: rgb(0, 128, 0);">struct </span><a name="_a0" style="color: rgb(61, 87, 140);"></a><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/structbuffer__data.html" style="color: rgb(70, 101, 162); text-decoration: none;">buffer_data</a> {</div><div class="line" style="font-stretch: normal; font-size: 13px; line-height: 1; font-family: monospace, fixed; min-height: 13px; white-space: pre-wrap; word-wrap: break-word; text-indent: -53px; padding-left: 53px; padding-bottom: 0px; margin: 0px; transition-property: background-color, box-shadow; transition-duration: 0.5s;">    <a name="a1" style="color: rgb(61, 87, 140);"></a><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/audio__convert_8c.html#ae1affc9ca37cfb624959c866a73f83c2" style="color: rgb(70, 101, 162); text-decoration: none;">uint8_t</a> *<a name="a2" style="color: rgb(61, 87, 140);"></a><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/structbuffer__data.html#a017ad9c72fdc7666c29841ddeefc8cd8" style="color: rgb(70, 101, 162); text-decoration: none;">ptr</a>;</div><div class="line" style="font-stretch: normal; font-size: 13px; line-height: 1; font-family: monospace, fixed; min-height: 13px; white-space: pre-wrap; word-wrap: break-word; text-indent: -53px; padding-left: 53px; padding-bottom: 0px; margin: 0px; transition-property: background-color, box-shadow; transition-duration: 0.5s;">    <span class="keywordtype" style="color: rgb(96, 64, 32);">size_t</span> <a name="a3" style="color: rgb(61, 87, 140);"></a><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/structbuffer__data.html#a9ca6d8ef5edd7f113c04c865f9367f8c" title="size left in the buffer" style="color: rgb(70, 101, 162); text-decoration: none;">size</a>; <span class="comment" style="color: rgb(128, 0, 0);">///< size left in the buffer</span></div><div class="line" style="font-stretch: normal; font-size: 13px; line-height: 1; font-family: monospace, fixed; min-height: 13px; white-space: pre-wrap; word-wrap: break-word; text-indent: -53px; padding-left: 53px; padding-bottom: 0px; margin: 0px; transition-property: background-color, box-shadow; transition-duration: 0.5s;"><span class="comment" style="color: rgb(128, 0, 0);"></span>};</div><div class="line" style="font-stretch: normal; font-size: 13px; line-height: 1; font-family: monospace, fixed; min-height: 13px; white-space: pre-wrap; word-wrap: break-word; text-indent: -53px; padding-left: 53px; padding-bottom: 0px; margin: 0px; transition-property: background-color, box-shadow; transition-duration: 0.5s;"></div><div class="line" style="font-stretch: normal; font-size: 13px; line-height: 1; font-family: monospace, fixed; min-height: 13px; white-space: pre-wrap; word-wrap: break-word; text-indent: -53px; padding-left: 53px; padding-bottom: 0px; margin: 0px; transition-property: background-color, box-shadow; transition-duration: 0.5s;"><span class="keyword" style="color: rgb(0, 128, 0);">static</span> <span class="keywordtype" style="color: rgb(96, 64, 32);">int</span> <a name="a4" style="color: rgb(61, 87, 140);"></a><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/avio__reading_8c.html#a3a86bff38e7cb9093d0a32316b73b29c" style="color: rgb(70, 101, 162); text-decoration: none;">read_packet</a>(<span class="keywordtype" style="color: rgb(96, 64, 32);">void</span> *opaque, <a class="code" href="https://www.ffmpeg.org/doxygen/2.3/audio__convert_8c.html#ae1affc9ca37cfb624959c866a73f83c2" style="color: rgb(70, 101, 162); text-decoration: none;">uint8_t</a> *<a name="a5" style="color: rgb(61, 87, 140);"></a><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/avisynth__c_8h.html#a5bc5fa69bee375df074734a2c4858604" style="color: rgb(70, 101, 162); text-decoration: none;">buf</a>, <span class="keywordtype" style="color: rgb(96, 64, 32);">int</span> buf_size)</div><div class="line" style="font-stretch: normal; font-size: 13px; line-height: 1; font-family: monospace, fixed; min-height: 13px; white-space: pre-wrap; word-wrap: break-word; text-indent: -53px; padding-left: 53px; padding-bottom: 0px; margin: 0px; transition-property: background-color, box-shadow; transition-duration: 0.5s;">{</div><div class="line" style="font-stretch: normal; font-size: 13px; line-height: 1; font-family: monospace, fixed; min-height: 13px; white-space: pre-wrap; word-wrap: break-word; text-indent: -53px; padding-left: 53px; padding-bottom: 0px; margin: 0px; transition-property: background-color, box-shadow; transition-duration: 0.5s;">    <span class="keyword" style="color: rgb(0, 128, 0);">struct </span><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/structbuffer__data.html" style="color: rgb(70, 101, 162); text-decoration: none;">buffer_data</a> *bd = (<span class="keyword" style="color: rgb(0, 128, 0);">struct </span><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/structbuffer__data.html" style="color: rgb(70, 101, 162); text-decoration: none;">buffer_data</a> *)opaque;</div><div class="line" style="font-stretch: normal; font-size: 13px; line-height: 1; font-family: monospace, fixed; min-height: 13px; white-space: pre-wrap; word-wrap: break-word; text-indent: -53px; padding-left: 53px; padding-bottom: 0px; margin: 0px; transition-property: background-color, box-shadow; transition-duration: 0.5s;">    buf_size = <a name="a6" style="color: rgb(61, 87, 140);"></a><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/common_8h.html#a0f84cbd7fd810cd678157c98cbfad4b0" style="color: rgb(70, 101, 162); text-decoration: none;">FFMIN</a>(buf_size, bd-><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/structbuffer__data.html#a9ca6d8ef5edd7f113c04c865f9367f8c" title="size left in the buffer" style="color: rgb(70, 101, 162); text-decoration: none;">size</a>);</div><div class="line" style="font-stretch: normal; font-size: 13px; line-height: 1; font-family: monospace, fixed; min-height: 13px; white-space: pre-wrap; word-wrap: break-word; text-indent: -53px; padding-left: 53px; padding-bottom: 0px; margin: 0px; transition-property: background-color, box-shadow; transition-duration: 0.5s;"></div><div class="line" style="font-stretch: normal; font-size: 13px; line-height: 1; font-family: monospace, fixed; min-height: 13px; white-space: pre-wrap; word-wrap: break-word; text-indent: -53px; padding-left: 53px; padding-bottom: 0px; margin: 0px; transition-property: background-color, box-shadow; transition-duration: 0.5s;">    printf(<span class="stringliteral" style="color: rgb(0, 32, 128);">"ptr:%p size:%zu\n"</span>, bd-><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/structbuffer__data.html#a017ad9c72fdc7666c29841ddeefc8cd8" style="color: rgb(70, 101, 162); text-decoration: none;">ptr</a>, bd-><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/structbuffer__data.html#a9ca6d8ef5edd7f113c04c865f9367f8c" title="size left in the buffer" style="color: rgb(70, 101, 162); text-decoration: none;">size</a>);</div><div class="line" style="font-stretch: normal; font-size: 13px; line-height: 1; font-family: monospace, fixed; min-height: 13px; white-space: pre-wrap; word-wrap: break-word; text-indent: -53px; padding-left: 53px; padding-bottom: 0px; margin: 0px; transition-property: background-color, box-shadow; transition-duration: 0.5s;"></div><div class="line" style="font-stretch: normal; font-size: 13px; line-height: 1; font-family: monospace, fixed; min-height: 13px; white-space: pre-wrap; word-wrap: break-word; text-indent: -53px; padding-left: 53px; padding-bottom: 0px; margin: 0px; transition-property: background-color, box-shadow; transition-duration: 0.5s;">    <span class="comment" style="color: rgb(128, 0, 0);">/* copy internal buffer data to buf */</span></div><div class="line" style="font-stretch: normal; font-size: 13px; line-height: 1; font-family: monospace, fixed; min-height: 13px; white-space: pre-wrap; word-wrap: break-word; text-indent: -53px; padding-left: 53px; padding-bottom: 0px; margin: 0px; transition-property: background-color, box-shadow; transition-duration: 0.5s;">    memcpy(buf, bd-><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/structbuffer__data.html#a017ad9c72fdc7666c29841ddeefc8cd8" style="color: rgb(70, 101, 162); text-decoration: none;">ptr</a>, buf_size);</div><div class="line" style="font-stretch: normal; font-size: 13px; line-height: 1; font-family: monospace, fixed; min-height: 13px; white-space: pre-wrap; word-wrap: break-word; text-indent: -53px; padding-left: 53px; padding-bottom: 0px; margin: 0px; transition-property: background-color, box-shadow; transition-duration: 0.5s;">    bd-><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/structbuffer__data.html#a017ad9c72fdc7666c29841ddeefc8cd8" style="color: rgb(70, 101, 162); text-decoration: none;">ptr</a>  += buf_size;</div><div class="line" style="font-stretch: normal; font-size: 13px; line-height: 1; font-family: monospace, fixed; min-height: 13px; white-space: pre-wrap; word-wrap: break-word; text-indent: -53px; padding-left: 53px; padding-bottom: 0px; margin: 0px; transition-property: background-color, box-shadow; transition-duration: 0.5s;">    bd-><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/structbuffer__data.html#a9ca6d8ef5edd7f113c04c865f9367f8c" title="size left in the buffer" style="color: rgb(70, 101, 162); text-decoration: none;">size</a> -= buf_size;</div><div class="line" style="font-stretch: normal; font-size: 13px; line-height: 1; font-family: monospace, fixed; min-height: 13px; white-space: pre-wrap; word-wrap: break-word; text-indent: -53px; padding-left: 53px; padding-bottom: 0px; margin: 0px; transition-property: background-color, box-shadow; transition-duration: 0.5s;"></div><div class="line" style="font-stretch: normal; font-size: 13px; line-height: 1; font-family: monospace, fixed; min-height: 13px; white-space: pre-wrap; word-wrap: break-word; text-indent: -53px; padding-left: 53px; padding-bottom: 0px; margin: 0px; transition-property: background-color, box-shadow; transition-duration: 0.5s;">    <span class="keywordflow" style="color: rgb(224, 128, 0);">return</span> buf_size;</div><div class="line" style="font-stretch: normal; font-size: 13px; line-height: 1; font-family: monospace, fixed; min-height: 13px; white-space: pre-wrap; word-wrap: break-word; text-indent: -53px; padding-left: 53px; padding-bottom: 0px; margin: 0px; transition-property: background-color, box-shadow; transition-duration: 0.5s;">}</div></div><div><br></div><div>When I debug my code, the function is called even after the data was all read. It is called more than a time with the buffer size is zero and later on the ptr itself is null. My question here is why does that happen?</div><div><br></div><div>The second question, is how to free the buffer allocated. In my application I require reading a JPEG frame from a camera that is considerable in size. What I do, is  that I free the AVIOContext and it's buffer just after I call avformat_open_input. But I can see the memory usage increasing by an estimate of the frame size each run.</div><div><br></div><div>Regards.</div><div class="line" style="font-stretch: normal; font-size: 13px; line-height: 1; font-family: monospace, fixed; min-height: 13px; white-space: pre-wrap; word-wrap: break-word; text-indent: -53px; padding-left: 53px; padding-bottom: 0px; margin: 0px; transition-property: background-color, box-shadow; transition-duration: 0.5s;"><br></div><div class="line" style="font-stretch: normal; font-size: 13px; line-height: 1; font-family: monospace, fixed; min-height: 13px; white-space: pre-wrap; word-wrap: break-word; text-indent: -53px; padding-left: 53px; padding-bottom: 0px; margin: 0px; transition-property: background-color, box-shadow; transition-duration: 0.5s;"><br></div><div class="line" style="font-stretch: normal; font-size: 13px; line-height: 1; font-family: monospace, fixed; min-height: 13px; white-space: pre-wrap; word-wrap: break-word; text-indent: -53px; padding-left: 53px; padding-bottom: 0px; margin: 0px; transition-property: background-color, box-shadow; transition-duration: 0.5s;"><br></div></div>
                                          </div></body>
</html>