]> git.sesse.net Git - xml-template/blobdiff - c++0x/xml-template.cpp
Fix leak of the temporary clone root elements in the C++0x version.
[xml-template] / c++0x / xml-template.cpp
index f28b3dc93cd00e67fcefb0695576056330ae9a24..96f027033ba30acf87ca50af3c6a22a49ed03ef8 100644 (file)
@@ -16,6 +16,7 @@ void clean_node(xmlNode *node)
                xmlNode *frag = xmlNewDocFragment(node->doc);
                xmlReplaceNode(node, frag);
                frag->children = node->children;
+               frag->last = node->last;
        }
 }
 
@@ -56,9 +57,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);
@@ -94,20 +98,19 @@ void Substitute::process(xmlNode *node, bool clean)
                if (child->type == XML_ELEMENT_NODE) {
                        // Find the ID, if any.
                        string id;
+                       xmlAttr *id_attr = NULL;
                        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));
-
-                                       if (clean) {
-                                               if (attr->prev == NULL) {
-                                                       child->properties = attr->next;
-                                               } else {
-                                                       attr->prev->next = attr->next;
-                                               }
-                                       }
+                                       xmlChar *id_buf = xmlNodeGetContent(attr->children);
+                                       id = reinterpret_cast<const char *>(id_buf);
+                                       xmlFree(id_buf);
+                                       id_attr = attr;
                                }
                        }
+                       if (clean && id_attr != NULL) {
+                               xmlRemoveProp(id_attr);
+                       }
 
                        // Check all substitutions to see if we found anything appropriate.
                        for (auto it : substitution_map) {