|
From: Tom H. <th...@cy...> - 2004-06-12 16:10:16
|
In message <40C...@in...>
Salvador Eduardo Tropea <sal...@in...> wrote:
> That´s because buffer will be invalid as soon as you leave "Function"
> and putenv have a reference and not a copy.
> So you have to do something like it:
>
> void Function(char *val)
> {
> char buffer[20];
> snprintf(buffer,20,"VAR=%s",val);
> putenv(strdup(buffer));
> }
>
> But the valgrind is quite sure you leaked.
The reason for this appears to be that although libc takes a copy
of the pointer the structure in which that pointer is held is freed
when the programs exits and valgrind calls __libc_freeres to make
libc free it's resources.
It doesn't free the pointer you passed to putenv however as it can't
know how it was allocated. If you run with --run-libc-freeres=no then
you'll find that valgrind doesn't complain. That might cause other
problems however.
> What´s the solution?
I'm not sure there is a good one I'm afraid.
Tom
--
Tom Hughes (th...@cy...)
Software Engineer, Cyberscience Corporation
http://www.cyberscience.com/
|