]> git.sesse.net Git - xml-template/blobdiff - c++0x/xml-template.cpp
Fix the last memory leaks from the clone test.
[xml-template] / c++0x / xml-template.cpp
index e6e88a30a31e1347deb1ab3b3e78bac34bd72493..c1d43edb421bada23f539026b60f60577d6f5124 100644 (file)
@@ -17,6 +17,9 @@ void clean_node(xmlNode *node)
                xmlReplaceNode(node, frag);
                frag->children = node->children;
                frag->last = node->last;
+
+               node->children = node->last = NULL;
+               xmlFreeNode(node);
        }
 }
 
@@ -57,9 +60,12 @@ void Clone::process(xmlNode *node, bool clean)
                xmlNode *new_node;
                xmlDOMWrapCloneNode(NULL, node->doc, node, &new_node, node->doc, NULL, 1, 0);
                it->process(new_node, clean);
-               for (xmlNode *child = new_node->children; child != NULL; child = child->next) {
+               while (new_node->children != NULL) {
+                       xmlNode *child = new_node->children;
+                       xmlUnlinkNode(child);
                        new_nodes.push_back(child);
                }
+               xmlFreeNode(new_node);
        }
 
        xmlFreeNodeList(node->children);
@@ -89,7 +95,9 @@ Substitute::~Substitute()
 
 void Substitute::process(xmlNode *node, bool clean)
 {
-       for (xmlNode *child = node->children; child != NULL; child = child->next) {
+       xmlNode *next_child;
+       for (xmlNode *child = node->children; child != NULL; child = next_child) {
+               next_child = child->next;       
                bool processed = false;
 
                if (child->type == XML_ELEMENT_NODE) {
@@ -99,7 +107,9 @@ void Substitute::process(xmlNode *node, bool clean)
                        for (xmlAttr *attr = child->properties; attr != NULL; attr = attr->next) {
                                if (strcmp(reinterpret_cast<const char *>(attr->ns->href), "http://template.sesse.net/") == 0 &&
                                    strcmp(reinterpret_cast<const char *>(attr->name), "id") == 0) {
-                                       id = reinterpret_cast<const char *>(xmlNodeGetContent(attr->children));
+                                       xmlChar *id_buf = xmlNodeGetContent(attr->children);
+                                       id = reinterpret_cast<const char *>(id_buf);
+                                       xmlFree(id_buf);
                                        id_attr = attr;
                                }
                        }
@@ -117,7 +127,7 @@ void Substitute::process(xmlNode *node, bool clean)
                                }
                        }
                }
-               
+
                if (!processed) {
                        process(child, clean);
                }