David Bertoni wrote:
> 
> 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
>>>
>>>
>>
> 

Hi Dave,

Thank you for your suggestions. I have tried them out, but unfortunately
they are not solving the problem. I have included the code here again to
show what is and is not causing the leak:

        // as before, PDoc is a member of class 'Message'
        std::string Message::convertToString(void) 
        {       
                DOMImplementation* impl = 
DOMImplementation::getImplementation(); 

                DOMLSSerializer   *theSerializer =
((DOMImplementationLS*)impl)->createLSSerializer(); 

                XMLCh* toTranscode = theSerializer->writeToString(pDoc); // 
remove to stop
leak
                std::string transcodedStr;
                transcodedStr = std::string(XMLString::transcode(toTranscode)); 
// remove
to stop leak

                XMLString::release(&toTranscode); // remove to stop leak
                XMLPlatformUtils::fgMemoryManager->deallocate(toTranscode); // 
remove to
stop leak
                theSerializer->release(); 
                return transcodedStr; 
        } 
I am calling this function 1000 times in my test, and it is leaking almost
1MB of memory. When I remove the call to writeToString(), it does not leak a
single byte.

Has anyone any ideas, or has anyone had any problems with writeToString() in
the past?

Thanks you for your replies.
Brendan

-- 
View this message in context: 
http://old.nabble.com/writeToString-memory-Leak-tp27720774p27741909.html
Sent from the Xerces - C - Users mailing list archive at Nabble.com.

Reply via email to