<div dir="ltr">Thanks Maxim,<div>Thanks for the suggestion about using ngx_pool_cleanup_add. Since this is external library memory I couldn't use ngx_pcalloc. Thanks again.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Nov 10, 2021 at 7:47 PM Maxim Dounin <<a href="mailto:mdounin@mdounin.ru">mdounin@mdounin.ru</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">Hello!<br>
<br>
On Wed, Nov 10, 2021 at 07:22:50PM -0800, Dk Jack wrote:<br>
<br>
> Hi,<br>
> In my module, I am allocating some memory for each request and saving the<br>
> ptr in my module context. I am registering a callback using<br>
> ngx_http_cleanup_add to perform cleanup for each request. I am releasing<br>
> the memory for the allocation in the cleanup callback. In the log phase, I<br>
> want to make available the contents of the memory I allocated as an nginx<br>
> variable.<br>
> <br>
> However, when I try to print this variable in the log, the memory has<br>
> already been de-allocated. It looks like the clean-up handlers are called<br>
> before the log phase. Since I have released the memory in the cleanup<br>
> handler, I have nothing to log in the log phase. Is there a way to defer<br>
> the calling of the clean-up handler until after the log phase?<br>
<br>
Request cleanup handlers as allocated by ngx_http_cleanup_add() <br>
are expected to be called before logging: these are used to stop <br>
various activities which might still be going on when nginx <br>
decides to terminate the request. Notably, it used by the <br>
upstream module to close upstream connections.<br>
<br>
If you want to free the memory, a better idea would be to use pool <br>
cleanup handlers instead, which are registered by the <br>
ngx_pool_cleanup_add() function. These are called while actually <br>
freeing request memory, after all request processing is completely <br>
finished, including logging. See here for details:<br>
<br>
<a href="http://nginx.org/en/docs/dev/development_guide.html#memory_management" rel="noreferrer" target="_blank">http://nginx.org/en/docs/dev/development_guide.html#memory_management</a><br>
<br>
Note well that for per-request allocations it is usually better to <br>
simply allocate memory from the request pool via ngx_palloc(), so <br>
it is freed automatically when the request pool is destroyed. <br>
Pool cleanup handlers are mostly needed to free various external <br>
resources, such as file descriptors or memory from external <br>
libraries.<br>
<br>
-- <br>
Maxim Dounin<br>
<a href="http://mdounin.ru/" rel="noreferrer" target="_blank">http://mdounin.ru/</a><br>
_______________________________________________<br>
nginx-devel mailing list<br>
<a href="mailto:nginx-devel@nginx.org" target="_blank">nginx-devel@nginx.org</a><br>
<a href="http://mailman.nginx.org/mailman/listinfo/nginx-devel" rel="noreferrer" target="_blank">http://mailman.nginx.org/mailman/listinfo/nginx-devel</a><br>
</blockquote></div>