On 2/26/2010 12:24 PM, brendanr wrote:

Hi Dave,

Thank you for your reply.

You are correct that I need to release the XMLCh* that is returned from
transcode. But this is not the leak.
The reason I didn't show the release in the code snippet I posted, is
because in reality I have a class that does this transcoding, which does the
release in the destructor. I didn't want to post it because it is only extra
code to look at.

The leak is definitely in the write to string. If you write a simple .exe
and call this function multiple times, you will see your memory usage
increase dramatically. Take out the call to writeToString(), and  it doesn't
increase at all.

This is quite a big issue for me, as I have an application that is creating
messages in XML and sending them between processes, so this gets called
quite a lot.
Well, the documentation for DOMLSSerializer states the following, regarding the second parameter:

@param manager The memory manager to be used to allocate the result string. If NULL is used, the memory manager used to construct the serializer will be used.

So it sounds to me like you just need to free the string using the MemoryManager instance used to create the DOMLSSerializer instance. Looking at DOMImplementationLS::createLSSerializer, I see:

virtual DOMLSSerializer* createLSSerializer(MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager) = 0;

Unless you specified a MemoryManager in the call, it's using the default instance, so this should work:

XMLPlatformUtils::fgMemoryManager->deallocate(toTranscode);

I supposed the documentation could more explicit that it's the caller's responsibility to release the string, but there is at least a hint there.

Dave



Thanks,
Brendan


David Bertoni wrote:

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