You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by Jerald Dawson <jd...@mrk.com> on 2000/10/05 21:12:12 UTC

XMLString::transcode Question

According to the docs, the char* returned by XMLString::transcode is the
responsiblity of the user to free. That much I understand. My question is
this. I have the following code:

CString* u = new CString(XMLString::transcode(anXMLChVar));

Now, does the CString object own the memory that transcode returns after
this call or do I still need to free the memory manually? In other words,
should I do this:

char* myStr = XMLString::transcode(anXMLChVar);
CString* u = new CString(myStr);
free(myStr);

This is with VC++. Just curious if someone knows the answer to this.

thanx

Jerald Dawson


Re: XMLString::transcode Question

Posted by Bob Kline <bk...@rksystems.com>.
On Thu, 5 Oct 2000, Jerald Dawson wrote:

> According to the docs, the char* returned by XMLString::transcode is the
> responsiblity of the user to free. That much I understand. My question is
> this. I have the following code:
> 
> CString* u = new CString(XMLString::transcode(anXMLChVar));
> 
> Now, does the CString object own the memory that transcode returns after
> this call or do I still need to free the memory manually? In other words,
> should I do this:
> 
> char* myStr = XMLString::transcode(anXMLChVar);
> CString* u = new CString(myStr);
> free(myStr);
> 
> This is with VC++. Just curious if someone knows the answer to this.

The CString constructor has no way of knowing where the memory for its
argument came from, so it assumes no responsibility for freeing that
memory (for all it knows, it could have been given what the C standard
calls a "string literal," which lives in static memory).  So, yes, if it
needs to be freed, you need to do it yourself.

-- 
Bob Kline
mailto:bkline@rksystems.com
http://www.rksystems.com