You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@httpd.apache.org by Weiming Yin <yi...@gmail.com> on 2009/02/20 04:14:45 UTC

A question about cross request memory/pool usage

Hi

I am writing an Apache module, I wanna setup some configure variable in the
module via a http request. The variable saved into a apr_hash_t in my own
module configure. But in the next request (the fixup handler), the one with
saved into the apr_hash_t is disappeared. I don't know why, is the memory
pool usage issue?

Thanks.

The sample code likes

1, My own module configureation
typedef struct {
    apr_hash_t   *var_hash;
} my_own_cfg_t;

2, Initial the configure in the creation handler

static void *create_antibot_cfg(apr_pool_t *p, server_rec *s) {
    my_own_cfg_t *cfg;

    cfg = (my_own_cfg_t *) apr_pcalloc(p, sizeof(my_own_cfg_t));
    cfg->var_hash = apr_hash_make(p);

    return (void *) cfg;
}

3, create var in request handler

...
var_t *var = apr_palloc(r->pool, sizeof(var_t));
apr_hash_set(cfg->var_hash, var->key, APR_HASH_KEY_STRING, var);

/* apr_hash_count(cfg->black_hash));     */ /* Shows 1 */

...

4, read the var_hash in another request

/* apr_hash_count(cfg->black_hash));     */ /* Always shows 0 */


-- 
Weiming Yin

Re: A question about cross request memory/pool usage

Posted by Weiming Yin <yi...@gmail.com>.
Hi, Nick

On Fri, Feb 20, 2009 at 3:42 PM, Nick Kew <ni...@webthing.com> wrote:

>
> On 20 Feb 2009, at 03:14, Weiming Yin wrote:
>
>  Hi
>>
>
> [answering here, but in future please post questions like this to the
> modules dev list]
>
>
>> I am writing an Apache module, I wanna setup some configure variable in
>> the module via a http request. The variable saved into a apr_hash_t in my
>> own module configure. But in the next request (the fixup handler), the one
>> with saved into the apr_hash_t is disappeared. I don't know why, is the
>> memory pool usage issue?
>>
>
> Most likely your requests are running in different processes, though you
> don't
> give enough information to be certain.
>
>  ...
>> var_t *var = apr_palloc(r->pool, sizeof(var_t));
>> apr_hash_set(cfg->var_hash, var->key, APR_HASH_KEY_STRING, var);
>>
>
> That's problematic for several reasons.   r->pool doesn't have the lifetime
> you want.
> The server pool does, but allocating from it in a request is a memory leak
> and not
> thread-safe.  Likewise setting cfg->hash there is not thread-safe.


I tested the r->server->process->pool, but I got a very strange result.
Sometimes (or some requests) it gives me some right result (the module
remember the variables into the hash), but sometimes it not. I donot know
why.


>
>
> Would this be a good time to mention that my book has a Chinese
> translation?


Your book, the new one? Which one, I got a Chinese translated <The Apache
Modules Book> on my hand.
I can find some friends to ask them if the have time and we can translate
the book into Chinese.


>
> --
> Nick Kew
>
> Application Development with Apache - the Apache Modules Book
> http://www.apachetutor.org/
>



-- 
Weiming Yin

Re: A question about cross request memory/pool usage

Posted by Nick Kew <ni...@webthing.com>.
On 20 Feb 2009, at 03:14, Weiming Yin wrote:

> Hi

[answering here, but in future please post questions like this to the
modules dev list]

>
> I am writing an Apache module, I wanna setup some configure  
> variable in the module via a http request. The variable saved into  
> a apr_hash_t in my own module configure. But in the next request  
> (the fixup handler), the one with saved into the apr_hash_t is  
> disappeared. I don't know why, is the memory pool usage issue?

Most likely your requests are running in different processes, though  
you don't
give enough information to be certain.

> ...
> var_t *var = apr_palloc(r->pool, sizeof(var_t));
> apr_hash_set(cfg->var_hash, var->key, APR_HASH_KEY_STRING, var);

That's problematic for several reasons.   r->pool doesn't have the  
lifetime you want.
The server pool does, but allocating from it in a request is a memory  
leak and not
thread-safe.  Likewise setting cfg->hash there is not thread-safe.

Would this be a good time to mention that my book has a Chinese  
translation?

-- 
Nick Kew

Application Development with Apache - the Apache Modules Book
http://www.apachetutor.org/