Re: [xml] Do I need to free existing content before overwriting with a new string?



On Sat, Jun 11, 2011 at 12:46:14PM +0100, Angus Comber wrote:
Hello

I need to update the value of a particular key.  Using the libxml tutorial
Appendix E as a starting point I changed parsestory as below to update the
contents of the node with key=keyword.

Will this code leak memory?  Do I need to free the existing contents before
overwriting? Here is code:


void parseStory (xmlDocPtr doc, xmlNodePtr cur, char *keyword) {
cur = cur->xmlChildrenNode;
while (cur != NULL) {
if ((!xmlStrcmp(cur->name, (const xmlChar *)"keyword"))){
            cur = cur->children;
            cur->content = xmlStrndup((const xmlChar *)"mynewvalue",
strlen("mynewvalue"));
}

  yes you would have to xmlFree(cur->content) assuming it's not NULL
before rewriting it

Daniel


-- 
Daniel Veillard      | libxml Gnome XML XSLT toolkit  http://xmlsoft.org/
daniel veillard com  | Rpmfind RPM search engine http://rpmfind.net/
http://veillard.com/ | virtualization library  http://libvirt.org/



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