]> git.sesse.net Git - xml-template/blobdiff - c++0x/xml-template.cpp
Fix the worst (but not all) memory leaks in the C++0x version.
[xml-template] / c++0x / xml-template.cpp
index 1e727613160acf49fb1ec860b88179b0fd7aecb5..a6a7316846db452bdf03e364f9f81a830486aa72 100644 (file)
@@ -21,6 +21,8 @@ void clean_node(xmlNode *node)
 
 }  // namespace
 
+Directive::~Directive() {}
+
 Replace::Replace(const string &str)
        : str(str) {}
 
@@ -31,10 +33,21 @@ void Replace::process(xmlNode *node, bool clean) {
        }
 }
 
-Clone::Clone(const std::vector<Directive *> &subdirectives)
+Clone::Clone(const vector<Directive *> &subdirectives)
+       : subdirectives(subdirectives) {}
+       
+Clone::Clone(initializer_list<Directive *> subdirectives)
        : subdirectives(subdirectives) {}
 
-void Clone::process(xmlNode *node, bool clean) {
+Clone::~Clone()
+{
+       for (auto it : subdirectives) {
+               delete it;
+       }
+}
+
+void Clone::process(xmlNode *node, bool clean)
+{
        // We can't use xmlNewDocFragment, since xmlDOMWrapCloneNode only knows
        // how to clone elements.
        vector<xmlNode *> new_nodes;
@@ -59,8 +72,19 @@ void Clone::process(xmlNode *node, bool clean) {
 
 Substitute::Substitute(const unordered_map<string, Directive*> &substitution_map)
        : substitution_map(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) {
+void Substitute::process(xmlNode *node, bool clean)
+{
        for (xmlNode *child = node->children; child != NULL; child = child->next) {
                bool processed = false;
 
@@ -111,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();