<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style>
</head>
<body class='hmmessage'>
Hello!<div><br></div><div>I am trying to create a custom AVIOContext so that I can "catch" the output of FFmpeg and direct it where I need it (not a file).  I have done this successfully to the point of generating a playable video file (which I am sending to a file during development, but won't later.)  After the file is generated, however, I run into memory cleanup problems in avio_close().  The crash occurs even if I immediately call avio_close() after avio_alloc_context(), and I think I have an idea why:</div><div><br></div><div>I am passing an 'opaque' pointer to the avio_alloc_context() / AVIOContext which points back to my redirecting class/object so that my callback functions know where their object lives.  This certainly appears to work for me, so I was assuming that was the purpose of 'opaque', and I have it pointing to my own object type.</div><div><br></div><div>However, when I look at the source code in aviobuf.c, avio_close(), we have:</div><div><br></div><div><div>int avio_close(AVIOContext *s)</div><div>{</div><div>    URLContext *h = s->opaque;</div><div><br></div><div>    av_free(s->buffer);</div><div>    av_free(s);</div><div>    return ffurl_close(h);</div><div>}</div></div><div><br></div><div>Note that opaque is expected to have type 'URLContext *'.  I tracked my memory crash to the ffurl_close() line, so:</div><div><br></div><div><div>int ffurl_close(URLContext *h)</div><div>{</div><div>    int ret = 0;</div><div>    if (!h) return 0; /* can happen when ffurl_open fails */</div><div><br></div><div>    if (h->is_connected && h->prot->url_close)</div><div>        ret = h->prot->url_close(h);</div><div>#if CONFIG_NETWORK</div><div>    ff_network_close();</div><div>#endif</div><div>    if (h->prot->priv_data_size)</div><div>        av_free(h->priv_data);</div><div>    av_free(h);</div><div>    return ret;</div><div>}</div></div><div><br></div><div>And my problem becomes rather obvious - AVIOContext->opaque is expected to point to a structure of type URLContext for this close routine, not to a custom type to be passed in to my callback routines.  The code crashes when it tries to access h->prot, although all of the ffurl_close() will be problematic unless h (which is AVIOContext->opaque) references a URLContext structure.</div><div><br></div><div>I have no idea what the url_open(), ffurl_...(), etc. routines are used for, and can't think of any reason that I need them in my project.  However, I do need the opaque pointer sent to my callback routines.</div><div><br></div><div>A workaround would be to avoid calling avio_close() and instead call av_free() directly on the buffer and AVIOContext.  However, before I resort to a workaround, I wanted to check my understanding of the situation against people more familiar with FFmpeg's code?  Can you guys please verify or correct my interpretation of the matter?</div><div><br></div><div>Thanks,</div><div>Wiley</div><div><br></div>                                        </body>
</html>