- Windows automatically copies and controls the memory with _putenv, allocating
a copy on Windows should not be done.
- Unix requires a memory allocation, you can free the memory in the environment
if you clear the environment variable.
e.g. UNIX
mymem=strdup("FOO=abc");
putenv(mymem);
putenv("FOO=");
free(mymem);
In my opinion it is not worth the coding effort to keep track of the
environment variables you set and then freeing them. Environment variables
should be set once and used for the life of the program they are not meant to
be set and unset over and over again.
UNIX should just be putenv(strdup("FOO=abc"));//set it and forget it.
-----Original Message-----
From: Graham Leggett [mailto:[email protected]]
Sent: Monday, March 29, 2010 9:55 AM
To: Dan Poirier
Cc: [email protected]
Subject: Re: apr_env_set use of putenv
On 29 Mar 2010, at 3:36 PM, Dan Poirier wrote:
> I don't think that's the right pattern to follow. apr_table is used
> to
> allocate a new data structure, owned by the caller, and the caller
> certainly should control its lifetime. apr_env_set() is used to add
> an
> entry to the OS's environment, which the caller does not own and would
> not expect to have any control over the lifetime of its entries.
From what I can see of the code right now, the caller is expected to
control the lifetime of the string that it passes, or set up their own
cleanup as appropriate to ensure that the environment entry is removed
if the pool is removed.
The strdup() is by definition a leak, so that isn't ideal at all.
I suspect the docs would need to be updated to warn the caller than if
they set a string in the environment, they are required to ensure
their string lives as long as the process, or to register their own
cleanup if not.
Regards,
Graham
--