memory leak in setenv()?
Corinna Vinschen
vinschen@cygnus.com
Tue Jul 25 03:27:00 GMT 2000
Andrej Borsenkow wrote:
>
> If existing value is longer than new, old value is simply overwritten.
> But if old value is shorter, new memory chunk is allocated via malloc(),
> but old one is never freed. Initially all environment strings are
> allocated using strdup() in environ_init(), so who is responsible for
> freeing unused memory in this case?
Nobody in case of setenv() and unsetenv(). It's a known problem
of the environment implementation that freeing environment
variables is not easy. The library can't decide if a string is
currently in use by the application.
As another example take a look into the implementation of
setenv/unsetenv in glibc. It does never free environment strings
by itself so each call to setenv(NAME, VAL, 1) creates a new
memory leak unless it is a known string value once given to
setenv. In that case, glibc tries to recycle strings to use
memory more efficient.
Corinna
--
Corinna Vinschen
Cygwin Developer
Cygnus Solutions, a Red Hat company
--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com
More information about the Cygwin
mailing list