]> git.sesse.net Git - xml-template/commitdiff
Fix the worst (but not all) memory leaks in the C++0x version.
authorsgunderson@bigfoot.com <>
Wed, 21 Sep 2011 21:32:16 +0000 (23:32 +0200)
committersgunderson@bigfoot.com <>
Wed, 21 Sep 2011 21:32:16 +0000 (23:32 +0200)
c++0x/xml-template.cpp
c++0x/xml-template.h

index ac686502afc6e06b064f04938bfcb69662e675ff..a6a7316846db452bdf03e364f9f81a830486aa72 100644 (file)
@@ -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<Directive *> &subdirectives)
 Clone::Clone(initializer_list<Directive *> 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<string, Directive*> &substitution_map
 Substitute::Substitute(initializer_list<pair<const string, Directive*>> 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();
index f0da0b8b0cdd1d007aca0b35f2b33457b2d47c3b..8f3d69b19257e3ae85e0d4f57163b39597277c30 100644 (file)
@@ -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<Directive *> &subdirectives);
        Clone(std::initializer_list<Directive *> subdirectives);
+       ~Clone();
        virtual void process(xmlNode *node, bool clean);
 
  private:
@@ -36,6 +38,7 @@ class Substitute : public Directive {
  public:
        Substitute(const std::unordered_map<std::string, Directive*> &substitution_map);
        Substitute(std::initializer_list<std::pair<const std::string, Directive*>> substitution_map);
+       ~Substitute();
 
        virtual void process(xmlNode *node, bool clean);