Description
Hi,
I wanted to use xerces and DOMDocument as kind of dynamical XML fragments container. I want to add new and remove old unused XML elements, but i have encountered that the memory usage is increasing without any apparent reason. If i want to remove the element i wanted to use following code:
DOMNode* pNode = pParent->removeChild(pElement);
pNode->release();
By the documentation, all the memory should be marked as usable for feature use, but that is not true, because the attributes are not released and memory usage is increasing. So I modified the code as follows:
DOMNode* pNode = pParent->removeChild(pElement);
DOMNamedNodeMap* pAttributes = pElement->getAttributes();
while(pAttributes->getLength() != 0)
pNode->release();
This code will release the node with its attributes, but still the memory usage is increasing. I have traced the xerces code and found where the problem lies. Its in the method void DOMBuffer::expandCapacity(const unsigned int extraNeeded) which is leaking. The easy solution could be to use the DOMBuffer* DOMDocumentImpl::popBuffer() in a way where the necessary buffer size will be specified and it will return sufficient buffer from fRecycleBufferPtr used or null to create new large enough buffer? I propose something like
DOMBuffer* DOMDocumentImpl::popBuffer(int nMinLength)
{
if (!fRecycleBufferPtr || fRecycleBufferPtr->empty())
return 0;
DOMBuffer* first = fRecycleBufferPtr->pop();
DOMBuffer* buffer = first;
do
{
if (buffer->getLen() >= nMinLength) return buffer;
else
}
while(buffer != first);
return 0;
}
By this code you can avoid the void DOMBuffer::expandCapacity(const unsigned int extraNeeded) call and the memory leaks. What do you think about it?
Sincerely,
Vit Ondruch