Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Huge memory leak with 'Xerces::getTextContent' #179

Open
alexzhornyak opened this issue May 21, 2018 · 0 comments
Open

Huge memory leak with 'Xerces::getTextContent' #179

alexzhornyak opened this issue May 21, 2018 · 0 comments

Comments

@alexzhornyak
Copy link
Contributor

alexzhornyak commented May 21, 2018

The problem with getTextContent lies in the memory management area. This function goes over child nodes accumulating text in a buffer which it returns to you at the end. Important part to know is that this buffer is allocated on the document heap and will only be freed when you destroy the document. Imagine an application that loads a DOM document at the beginning and then performs multiple queries (which involve calling getTextContent) on this single document.

In our case this is the function BasicContentExecutor::processScript

void BasicContentExecutor::processScript(XERCESC_NS::DOMElement* content) {
 	// contents were already downloaded in setupDOM, see to SCXML rec 5.8
	std::string scriptContent(X(content->getTextContent()));
	_callbacks->eval(scriptContent);
}

state_machine11

This simple example will cause huge memory leak. And the leak will be as big as the length of script's text.

So, we should change source code to

void BasicContentExecutor::processScript(XERCESC_NS::DOMElement* content) {
	// contents were already downloaded in setupDOM, see to SCXML rec 5.8
	
	std::string scriptContent("");

	DOMNode *node = content->getFirstChild();
	if (node && node->getNodeType() == DOMNode::TEXT_NODE) {
		char *cstr = XMLString::transcode(node->getNodeValue());
		scriptContent = cstr;
		XMLString::release(&cstr);
	}
	
	_callbacks->eval(scriptContent);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant