[xml] libxml2: xmlNewChield and memory control



Hi,

I'm pretty new to libxml2 (2.6.29) and have therefor a question about
memory handling. When I add a new child to a tree e.g.
xmlNewChild(root_node, NULL, BAD_CAST "test", BAD_CAST testcontent);
Does the function xmlNewChild copy the content "testcontent" to a new
buffer? So that a free(testcontent) is safe.

To make it more clear, here is an example:

doc = xmlNewDoc(BAD_CAST "1.0");
root_node = xmlNewNode(NULL, BAD_CAST "my_test");
xmlDocSetRootElement(doc, root_node);

char *content = get_a_string_from_wherever();
xmlNewChild(root_node, NULL, BAD_CAST "test", BAD_CAST content);
free(content);

/* do some more stuff */

xmlSaveFormatFileEnc(filename, doc, "UTF-8", 1);
xmlFreeDoc(doc);
xmlCleanupParser();
xmlMemoryDump();

If xmlNewChild wouldn`t copy the string into a new buffer then I would
free the memory and at least xmlSaveFormatFileEnc() should run into some
trouble. I wrote a little test program and it's working (after a free()
call). So I assume it's OK to call free() after xmlNewChild() but I
really want to make sure.

Can someone explain me the behavior of xmlNewChild()?

cheers,
Stefan




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