]> git.sesse.net Git - xml-template/blobdiff - c++0x/xml-template.cpp
Fix yet more memory leaks in the C++0x version.
[xml-template] / c++0x / xml-template.cpp
index ac686502afc6e06b064f04938bfcb69662e675ff..f28b3dc93cd00e67fcefb0695576056330ae9a24 100644 (file)
@@ -21,11 +21,13 @@ void clean_node(xmlNode *node)
 
 }  // namespace
 
+Directive::~Directive() {}
+
 Replace::Replace(const string &str)
        : str(str) {}
 
 void Replace::process(xmlNode *node, bool clean) {
-       node->children = xmlNewTextLen(reinterpret_cast<const xmlChar *>(str.data()), str.size());
+       xmlNodeSetContentLen(node, reinterpret_cast<const xmlChar *>(str.data()), str.size());
        if (clean) {
                clean_node(node);
        }
@@ -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
@@ -52,7 +61,10 @@ void Clone::process(xmlNode *node, bool clean)
                }
        }
 
-       node->children = NULL;
+       xmlFreeNodeList(node->children);
+       node->content = NULL;
+       node->children = node->last = NULL;
+
        for (auto child : new_nodes) {
                xmlAddChild(node, child);
        }       
@@ -67,6 +79,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 +138,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();