From 525aa8c4dc3e23f0369c7edb1fb507ecad5f43bf Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Wed, 23 Apr 2014 21:26:58 +0200 Subject: Fixed xerces DOM construction related memory leaks * Wrapped xerces DOM and support class instances in now DomDocumentGuard scope-guard class * FunctionReadDirectory class contains interal std::stack instance to store DomDocumentGuard instances ** wrapped in std::shared_ptr as FunctionReadDirectory is internally cloned by xalan... ** this is needed as the DOM has to preserved longer than the external function execution scope * Sadly XMLCh xerces strings have to be manually released ** added appropriate xercesc::XMLString::release calls * xalan::XercesDOMWrapperParsedSource does not mirror a given xerces DOM but convert it on instantiation ** this is why there is a dedicated finalize member method in InputXSLT::DomDocumentGuard * In short: I do not like the amount of trickery needed to simply prevent memory leaks in this context ** there sadly doesn't seem to be a substantially easier way to return arbitrary DOM trees from a external function --- src/support/dom_document_guard.cc | 45 +++++++++++++++++++++++++++++++++++++++ src/support/dom_document_guard.h | 36 +++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 src/support/dom_document_guard.cc create mode 100644 src/support/dom_document_guard.h (limited to 'src/support') diff --git a/src/support/dom_document_guard.cc b/src/support/dom_document_guard.cc new file mode 100644 index 0000000..a83345f --- /dev/null +++ b/src/support/dom_document_guard.cc @@ -0,0 +1,45 @@ +#include "support/dom_document_guard.h" + +#include +#include + +namespace InputXSLT { + +DomDocumentGuard::DomDocumentGuard(const std::string& rootNode): + parser_(), + dom_support_(parser_), + root_node_name_( + xercesc::XMLString::transcode(rootNode.data()) + ), + document_( + xercesc::DOMImplementation::getImplementation()->createDocument( + nullptr, + root_node_name_, + nullptr + ) + ), + parsed_source_() { } + +DomDocumentGuard::~DomDocumentGuard() { + xercesc::XMLString::release(&this->root_node_name_); + + this->document_->release(); +} + +xercesc::DOMDocument* DomDocumentGuard::operator->() { + return this->document_; +} + +xalan::XalanDocument* DomDocumentGuard::finalize() { + this->parsed_source_.reset( + new xalan::XercesDOMWrapperParsedSource( + document_, + parser_, + dom_support_ + ) + ); + + return this->parsed_source_->getDocument(); +} + +} diff --git a/src/support/dom_document_guard.h b/src/support/dom_document_guard.h new file mode 100644 index 0000000..decca01 --- /dev/null +++ b/src/support/dom_document_guard.h @@ -0,0 +1,36 @@ +#ifndef INPUTXSLT_SRC_SUPPORT_DOM_DOCUMENT_GUARD_H_ +#define INPUTXSLT_SRC_SUPPORT_DOM_DOCUMENT_GUARD_H_ + +#include +#include +#include + +#include + +#include +#include + +#include "common.h" + +namespace InputXSLT { + +class DomDocumentGuard { + public: + DomDocumentGuard(const std::string&); + ~DomDocumentGuard(); + + xercesc::DOMDocument* operator->(); + xalan::XalanDocument* finalize(); + + private: + xalan::XercesParserLiaison parser_; + xalan::XercesDOMSupport dom_support_; + XMLCh* root_node_name_; + xercesc::DOMDocument* const document_; + std::unique_ptr parsed_source_; + +}; + +} + +#endif // INPUTXSLT_SRC_SUPPORT_DOM_DOCUMENT_GUARD_H_ -- cgit v1.2.3