Topic: c_str() & Memory Leak
Author: jcoffin@taeus.com (Jerry Coffin)
Date: 1999/04/19 Raw View
In article <7f7vs8$9lk$1@nnrp1.dejanews.com>, richard_jim@my-
dejanews.com says...
> Hi, a little question about STL, Assume I have two functions below :
>
> const char *GetBuffer(string strInput)
> {
> string strNew("abc");
> strNew += strInput;
> return strNew.c_str();
This gets a pointer to the internal data of a string object, and
returns it. Unfortunately, in the process of returning from the
function, the string object gets destroyed, so you're returning a
dangling pointer.
> void StringProcess() { string strFromFile; // ... some omited code here to
> read data from certain file into strFromFile;
>
> const char *pszAny = GetBuffer(strFromFile);
When you get here, doing nearly ANYTHING with the value returned from
GetBuffer gives undefined results.
> So, the question is :
> (1) should I call free() in the StringProcess() to free the memory, will it
> cause any problem?
Yes, it will almost undoubtedly cause a problem -- the memory's
already been released (most likely with delete). It probably wasn't
even allocated with malloc/calloc in the first place, so calling free
on it would be a problem even if it hadn't already been freed.
> (2) if I remove free() in StringProcess(), any memory leakage will occur?
No -- the string object is being destroyed at the end of GetBuffer, so
there's no memory leakage. There IS a dangling pointer being returned
though...
> Any advice is welcome, thank you in advance !
My advice would be to post a more complete sample of what you're
trying to accomplish to comp.lang.c++. Then somebody can probably
help you keep your string as a string, rather than using a pointer to
char at all.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: richard_jim@my-dejanews.com
Date: 1999/04/18 Raw View
Hi, a little question about STL, Assume I have two functions below :
const char *GetBuffer(string strInput)
{
string strNew("abc");
strNew += strInput;
return strNew.c_str();
}
void StringProcess() { string strFromFile; // ... some omited code here to
read data from certain file into strFromFile;
const char *pszAny = GetBuffer(strFromFile);
// my question here is whether I have to free(pszAny) here or not
free(pszAny); // if I don't call free(), will this cause any memory leak
}
So, the question is :
(1) should I call free() in the StringProcess() to free the memory, will it
cause any problem?
(2) if I remove free() in StringProcess(), any memory leakage will occur?
Any advice is welcome, thank you in advance !
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1999/04/19 Raw View
richard_jim@my-dejanews.com wrote:
>
> Hi, a little question about STL, Assume I have two functions below :
>
> const char *GetBuffer(string strInput)
> {
> string strNew("abc");
> strNew += strInput;
> return strNew.c_str();
> }
No! strNew gets destroyed as soon as the function returns, invalidating
all pointers, references, and iterators into it. The value you're
returning is a pointer that will already have been deallocated by the
time you use it.
Now, if you were to change the return type to 'string', and return
strNew itself, that would be a different matter. The compiler can use
the returned value optimization, so that the implied wasteful copy
constructor call is eliminated. Wherever you store the returned string,
the memory would automatically be deallocated when that storage location
is destroyed.
> void StringProcess() { string strFromFile; // ... some omited code here to
> read data from certain file into strFromFile;
>
> const char *pszAny = GetBuffer(strFromFile);
>
> // my question here is whether I have to free(pszAny) here or not
> free(pszAny); // if I don't call free(), will this cause any memory leak
> }
>
> So, the question is :
> (1) should I call free() in the StringProcess() to free the memory, will it
> cause any problem?
Many. First of all, the memory allocated by 'string' is 'string's
responsibility to deallocate, not yours. Secondly, if deallocating it
were your responsibility, then you should probably do so with delete[],
because it was probably allocated with new[]. The 'new' and 'delete'
operators can be implemented using malloc() and free(), but it's not
safe to assume that they were.
Finally, as explained above, the memory has already been deallocated at
this point, so deallocating it again could potentially cause big
problems. On some systems, you could get spuriously acceptable results,
because the memory hasn't been reallocated yet, but that's nothing you
should count on.
> (2) if I remove free() in StringProcess(), any memory leakage will occur?
No, quite the contrary.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]