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

On Tue, Jul 27, 2004 at 07:11:53PM +0200, Martijn Faassen wrote:
Hi there,

I've spent quite a bit of trying to track down a segfault in my code, 
and I think I may have found the cause. The following test code:

#include "libxml/tree.h"

int main() {
  xmlDoc* doc1;
  xmlDoc* doc2;
  xmlNode* node;

  doc1 = xmlParseDoc("<doc>Foo</doc>");
  doc2 = xmlNewDoc("1.0");
  node = xmlDocGetRootElement(doc1);
  xmlDocSetRootElement(doc2, node);

doesn't seem to be safe; xmlFreeDoc(doc2) apparently tries to free 
something already freed in xmlFreeDoc(doc1). This is what valgrind says:
is moving nodes between documents in general not supported this way?

  Since doc1 and doc2 are not sharing their dictionnary, yes you have a problem.
If you're starting to move nodes between documents like that you can either
desactivate dictionnaries or make sure they share dictionnaries. Use one of
the more recent APIs like xmlReadDoc() and set XML_PARSE_NODICT option.


Daniel Veillard      | Red Hat Desktop team
veillard redhat com  | libxml GNOME XML XSLT toolkit | Rpmfind RPM search engine

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