On 2/26/2010 9:00 AM, brendanr wrote:

Hi,

I am using xerces C++ 3.1.0.
I have the following code that is causing a memory leak. I have looked
through previous posts regarding xmlToString, and the seem to say that
calling 'release()' on the Serializer fixes the memory leak.

Can anyone point out what I am doing incorrectly here? when I remove the
line of code containing writeToString, the leak disappears (but obviously I
am not getting my string!!)
Are you sure that's the only line of code you removed? What about the call to XMLString::transcode()?


//pDoc is a class member, and converToString is a class method
std::string convertToString(void)
{       
     // Factory method for getting a DOMImplementation object.
     // The DOM implementation retains ownership of the returned object.
     // Application code should NOT delete it.
     DOMImplementation* impl = DOMImplementation::getImplementation();

     // get a serializer, an instance of DOMLSSerializer        
     DOMLSSerializer   *theSerializer =
((DOMImplementationLS*)impl)->createLSSerializer();

     XMLCh* toTranscode= theSerializer->writeToString(pDoc);
     std::string transcodedStr =
std::string(XMLString::transcode(toTranscode));
The leak is here, in the call to XMLString::transcode(), because you need to call XMLString::release() on the pointer that's returned. Please read the documentation for XMLString::transcode() carefully.

char* transcodedData = XMLString::transcode();
const std::string transcodedStr(transcodedStr);
XMLString::release(&transcodedData);

...

Dave

Reply via email to