httpd-modules-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sindhi Sindhi <sindhi....@gmail.com>
Subject Re: Apache: Create server config only once
Date Sat, 25 May 2013 15:20:57 GMT
I tried to make the class MyFilterInit a Singleton class, but
when CreateServerConfig is called the second time, MyFilterInit constructor
is called is called second time, and here I see that the static instance of
MyFilterInit in the second call is set to NULL, which should have ideally
been a address that was set by the previous call.

Any help is highly appreciated.


On Sat, May 25, 2013 at 6:52 PM, Sindhi Sindhi <sindhi.for@gmail.com> wrote:

> Hi,
>
> I see that the create_server_config callback is called twice for every
> Apache server startup. I want to do a lot of initialization in this
> callback and allocations in this function are meant to be long lived. And
> these buffers are very huge, so I want to ensure these initializations are
> done only once per server start. As of now I'm trying to do the following -
>
> typedef struct
> {
> int bEnabled; // Enable or disable the module.
> MyFilterInit* myFilterInitObj; // A class that has methods to do all huge
> initializations
>  bool serverConfigured;
> } MyFilterConfig;
>
> static int serverConfigHit = 0;
>
> static void* CreateServerConfig(apr_pool_t* pool, server_rec* virtServer) {
>     MyFilterConfig *pExistingConfig = (MyFilterConfig *)
> ap_get_module_config (virtServer,  &tag_filter_module);
>
> if (serverConfigHit == 0) {
> MyFilterConfig *pConfig = (MyFilterConfig *) apr_pcalloc (pool, sizeof
> *pConfig);
>  pConfig->myFilterInitObj = new MyFilterInit(); // This does all the huge
> initializations
> serverConfigHit = serverConfigHit +1;
>  return pConfig;
> }
> return pExistingConfig;
> }
>
> But I see an issue here. The second time when CreateServerConfig is
> called,
> 1. pExistingConfig is not having the address which was set during the
> first call to CreateServerConfig
> 2. serverConfigHit is zero.
>
> Which means when CreateServerConfig was called first time, all the
> initializations I made is not recorded by the server. Which means all the
> allocations I made in the first call using malloc/new will result in a
> memory leak.
>
> Kindly advice how do I ensure that my initializations are performed only
> once.
>
> Thanks
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message