Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: setenv memory leak




On Oct 30, 2006, at 10:06 PM, email@hidden wrote:

Just documenting a bit for anybody out there running into the same problem
: g++ has a bit of a memory leak in its setenv function on os X (xcode
2.2)
Fbsd used to have it too, on my FreeBSD 6.1-RELEASE ws at work it seems to
be patched.


us = mktime(&loctime);
setenv("TZ", "GMT", 1);
struct tm *theirtime = localtime(&us);
setenv("TZ", "CET", 1);

BUGS section of the manpage for setenv(3).

Successive calls to setenv() or putenv() assigning a differently sized
value to the same name will result in a memory leak. The FreeBSD seman-
tics for these functions (namely, that the contents of value are copied
and that old values remain accessible indefinitely) make this bug
unavoidable. Future versions may eliminate one or both of these semantic
guarantees in order to fix the bug.


Also this comment in the source code: <http:// www.opensource.apple.com/darwinsource/10.4.7.ppc/Libc-391.2.6/stdlib/ FreeBSD/setenv.c.patch>
+ /* In UNIX03, we can't overwrite even if the string is long
+ * enough, because the putenv() string is owned by the user
+ * (ie, always malloc() a new string) */


And <http://www.opensource.apple.com/darwinsource/10.4.7.ppc/ Libc-391.2.6/stdlib/FreeBSD/setenv.c.>:

/* For non Unix03, or UnixO3 setenv(), we make a copy of the user's
* strings. For Unix03 putenv(), we put the string directly in
* the environment. */


There is definitely a documentation bug in the manpage, and the fact that it appears to be impossible to set an environment variable without leaking is either a bug in the standard or in someone's interpretation of it. Whatever, file a bug :)

Peter
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/xcode-users/email@hidden

This email sent to email@hidden
References: 
 >setenv memory leak (From: email@hidden)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.