Re: [xml] using xmlDocSetRootElement to move nodes between documents



Daniel Veillard wrote:
On Wed, Jul 28, 2004 at 12:07:28PM +0200, Martijn Faassen wrote:

int main() {
 xmlDoc* doc1;
 xmlDoc* doc2;
 xmlNode* node;
 xmlChar* text = "<doc>foo</doc>";
 char* url = "http://www.infrae.com";;
 doc1 = xmlReadDoc(text, url, NULL,
                   XML_PARSE_NOENT | XML_PARSE_NODICT | XML_PARSE_NONET);
 doc2 = xmlReadDoc(text, url, NULL,
                   XML_PARSE_NOENT | XML_PARSE_NODICT | XML_PARSE_NONET);
 node = xmlDocGetRootElement(doc1);
 xmlImportNode(doc2, node);
 xmlDocSetRootElement(doc2, node);
 xmlFreeDoc(doc1);
 xmlFreeDoc(doc2);
}

This still gives me valgrind invalid reads when doing the xmlFreeDoc().


==17123== Invalid read of size 4
==17123==    at 0x80988B4: xmlDictOwns (dict.c:709)
==17123==    by 0x805EA32: xmlFreeNodeList (tree.c:3294)
==17123==    by 0x805E9DB: xmlFreeNodeList (tree.c:3284)
==17123==    by 0x805C9A6: xmlFreeDoc (tree.c:1117)

 in xmlFreeNodeList cur->doc->dict is 0xffffffff with memory debug.
The node document still points to the old doc, which has been freed.
xmlDocSetRootElement() should recursively set the document for the subtree...


I tried adding a putting a xmlSetTreeDoc(node, doc2), which presumably should do that, but I still get this from valgrind:

==24774== Invalid read of size 4
==24774==    at 0x3C05FD64: xmlFreeNodeList__internal_alias (tree.c:3276)
==24774==    by 0x3C05FD9F: xmlFreeNodeList__internal_alias (tree.c:3286)
==24774==    by 0x3C05D72C: xmlFreeDoc__internal_alias (tree.c:1126)
==24774==    by 0x8048663: main (foo2.c:26)
==24774==  Address 0x3C163960 is 80 bytes inside a block of size 88 free'd
==24774==    at 0x3C01F918: free (vg_replace_malloc.c:127)
==24774==    by 0x3C05D6FD: xmlFreeDoc__internal_alias (tree.c:1124)
==24774==    by 0x8048658: main (foo2.c:25)

Regards,

Martijn



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]