[Glass] How can I release a malloced CByteArray ....
Martin McClure
martin.mcclure at gemtalksystems.com
Wed Feb 26 11:44:32 PST 2014
On 02/26/2014 09:16 AM, James Foster wrote:
> "The POSIX function |putenv()| is used to set environment variable
> values. The |putenv()| function does not create a copy of the string
> supplied to it as an argument; rather, it inserts a pointer to the
> string into the environment array.” In this situation the caller
> typically does a malloc() without a free().
>
> https://www.securecoding.cert.org/confluence/display/seccode/POS34-C.+Do+not+call+putenv()+with+a+pointer+to+an+automatic+variable+as+the+argument
So in GemStone, one should use #gcMalloc: if there is any chance that
the memory should be freed before the process exits.
But Marten has a point in that it is usually considered bad practice for
memory to be allocated by one component and freed by another. In the
putenv() example, it may well be that free() is never called by anyone,
if the putenv() call succeeds and the environment variable is used until
the process exits.
But there are cases where the memory used by putenv() *should* be freed,
and if so the component that malloced the memory should be the one to
free it. On case is shown in the code example on the cert.org page that
James references. If putenv() does not succeed, free() is called.
Another case is that if putenv() was called again with a different
string for the same variable, the previous string should be freed, or it
would be considered a memory leak.
Regards,
-Martin
More information about the Glass
mailing list