From 34c73f84835f00b960b1bde0aca69588230791a4 Mon Sep 17 00:00:00 2001 From: "sgunderson@bigfoot.com" <> Date: Wed, 21 Sep 2011 23:32:16 +0200 Subject: [PATCH] Fix the worst (but not all) memory leaks in the C++0x version. --- c++0x/xml-template.cpp | 17 +++++++++++++++++ c++0x/xml-template.h | 3 +++ 2 files changed, 20 insertions(+) diff --git a/c++0x/xml-template.cpp b/c++0x/xml-template.cpp index ac68650..a6a7316 100644 --- a/c++0x/xml-template.cpp +++ b/c++0x/xml-template.cpp @@ -21,6 +21,8 @@ void clean_node(xmlNode *node) } // namespace +Directive::~Directive() {} + Replace::Replace(const string &str) : str(str) {} @@ -37,6 +39,13 @@ Clone::Clone(const vector &subdirectives) Clone::Clone(initializer_list subdirectives) : subdirectives(subdirectives) {} +Clone::~Clone() +{ + for (auto it : subdirectives) { + delete it; + } +} + void Clone::process(xmlNode *node, bool clean) { // We can't use xmlNewDocFragment, since xmlDOMWrapCloneNode only knows @@ -67,6 +76,13 @@ Substitute::Substitute(const unordered_map &substitution_map Substitute::Substitute(initializer_list> substitution_map) : substitution_map(substitution_map) {} +Substitute::~Substitute() +{ + for (auto it : substitution_map) { + delete it.second; + } +} + void Substitute::process(xmlNode *node, bool clean) { for (xmlNode *child = node->children; child != NULL; child = child->next) { @@ -119,6 +135,7 @@ void process_file(const string &input_filename, xmlDocPtr doc = xmlParseFile(input_filename.c_str()); root_directive->process(xmlDocGetRootElement(doc), true); xmlSaveFile(output_filename.c_str(), doc); + xmlFreeDoc(doc); xmlCleanupParser(); xmlMemoryDump(); diff --git a/c++0x/xml-template.h b/c++0x/xml-template.h index f0da0b8..8f3d69b 100644 --- a/c++0x/xml-template.h +++ b/c++0x/xml-template.h @@ -10,6 +10,7 @@ class Directive { public: + virtual ~Directive(); virtual void process(xmlNode *node, bool clean) = 0; }; @@ -26,6 +27,7 @@ class Clone : public Directive { public: Clone(const std::vector &subdirectives); Clone(std::initializer_list subdirectives); + ~Clone(); virtual void process(xmlNode *node, bool clean); private: @@ -36,6 +38,7 @@ class Substitute : public Directive { public: Substitute(const std::unordered_map &substitution_map); Substitute(std::initializer_list> substitution_map); + ~Substitute(); virtual void process(xmlNode *node, bool clean); -- 2.39.2