What's the meaning ctx->srv conf = cscfp->ctx->srv conf

hi~~, now I’m reading the code of nginx-0.9.1.

I readed the code “ctx->srv_conf = cscfp[s]->ctx->srv_conf” at line
593 in the ngx_http.c .
but I really don’t understand it’s meaning.

Who can tell me why do this? I find it will make memory leak.

Posted at Nginx Forum:

hi, garry:

That code is for changing the context of ‘cf’, and make the ‘cf’
appropriate for the function ‘merge_srv_conf’. I can’t see any thing
matters
can cause memory leak unless the “merge_srv_conf” function that the
modules
provide did this.


I drawed a picture according to the code of the nginx-0.9.1. The top
part in the picture is built in the ngx_http_block of ngx_http.c.

When the ngx_http_merge_servers function at line 277 is called, the
ctx->srv_conf have hold the address of cf->ctx->srv_conf. After
ctx->srv_conf = cscfp[s]->ctx->srv_conf, ctx->srv_conf discard it’s
older address to point to the new address cscfp[s]->ctx->srv_conf.

So I think it will generate memory leak.

Posted at Nginx Forum:

On Thu, Dec 23, 2010 at 01:46:15AM -0500, garry.lgr wrote:


I drawed a picture according to the code of the nginx-0.9.1. The top
part in the picture is built in the ngx_http_block of ngx_http.c.

When the ngx_http_merge_servers function at line 277 is called, the
ctx->srv_conf have hold the address of cf->ctx->srv_conf. After
ctx->srv_conf = cscfp[s]->ctx->srv_conf, ctx->srv_conf discard it’s
older address to point to the new address cscfp[s]->ctx->srv_conf.

Think ctx->srv_conf like a hidden function parameter available via
cf->ctx:

 merge_srv_conf(cf, [ cf->ctx->srv_conf ], ...

So I think it will generate memory leak.

All memory operated in ngx_http.c and many other files are allocated
from memory pools freed at once. For example, memory in ngx_http.c
is freed after each reconfiguration.


Igor S.
http://sysoev.ru/en/

Thanks, Igor S…

Now I understood your meaning. As a hidden parameter, that’s very
smart.

but I looked up the code of many http module files, don’t find any
where you use this .

Could you tell me where you use it ?

Posted at Nginx Forum:

ooh, thanks Igor S… now I know it.

Posted at Nginx Forum:

On Thu, Dec 23, 2010 at 02:51:01AM -0500, garry.lgr wrote:

Thanks, Igor S…

Now I understood your meaning. As a hidden parameter, that’s very
smart.

I’m not sure that is smart at least in this context.
It appeared here rather for compatibility reason.

but I looked up the code of many http module files, don’t find any
where you use this .

Could you tell me where you use it ?

They are used via macros ngx_http_conf_get_module_srv_conf()
and ngx_http_conf_get_module_loc_conf() in merge phases.
Currenly only ngx_http_conf_get_module_loc_conf() is used in such
context
in proxy, fastcgi, uwscgi and scgi modules.


Igor S.
http://sysoev.ru/en/