I'm having trouble understanding Xerces-C++ memory management.
If I have this (example) XML file "config.xml":
<?xml version="1.0" encoding="UTF-8"?>
<settings>
<port>
<reference>Ref1</reference>
<label>1PPS A</label>
<enabled>true</enabled>
</port>
</settings>
and this code:
#include <xercesc/dom/DOM.hpp>
XERCES_CPP_NAMESPACE_USE
DOMElement *nextChildElement(const DOMElement *parent)
{
DOMNode *node = (DOMNode *)parent->getFirstChild();
while (node)
{
if (node->getNodeType() == DOMNode::ELEMENT_NODE)
return (DOMElement *)node;
node = node->getNextSibling();
}
return nullptr;
}
int main(int argc, char **argv)
{
XMLPlatformUtils::Initialize();
XMLCh tempStr[100];
XMLString::transcode("LS", tempStr, 99);
DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(tempStr);
DOMLSParser *parser = ((DOMImplementationLS*)impl)->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 0);
DOMDocument *doc = impl->createDocument(0, 0, 0);
doc = parser->parseURI("config.xml");
DOMElement *el = doc->getDocumentElement(); // <settings>
el = nextChildElement(el); // <port>
el = nextChildElement(el); // <reference>Ref1</reference>
// Heap blows up here
while (1) {
char *cstr = XMLString::transcode(el->getTextContent());
XMLString::release(&cstr); // cstr is "Ref1"
}
// and/or here
while (1) {
XMLCh *xstr = XMLString::replicate(el->getTextContent());
char *cstr = XMLString::transcode(xstr); // cstr is "Ref1"
XMLString::release(&cstr);
XMLString::release(&xstr);
}
}
Why does the program (heap) memory blow up in the while (1)
loops. Either loop results in the same memory problem:
Note: I'm using Visual Studio 2017, and I've tested this in these configurations (all with same results):
- xerces-c-3.2.1, static lib, x64
- xerces-c-3.2.1, dynamic (dll), x64
- xerces-c-3.1.2, static lib, x64