日期:2014-05-16  浏览次数:20625 次

libxml2 解析xml文档内存泄漏
我在接收线程中解析从服务器发送过来的xml数据流,解析程序如下:

int xml_parse(char *buf,int size, int index)
{

    int i,len,id;
    xmlDocPtr doc;
    xmlNodePtr curNode;

    doc = xmlParseMemory(buf,size);
    if (doc == NULL)
    {
        fprintf(stderr,"Document not parsed successfully.\n");
        return -1;
    }
    curNode = xmlDocGetRootElement(doc);        // get root element
    if (NULL == curNode)
    {
        fprintf(stderr,"empty document.\n");
        xmlFreeDoc(doc);
        return -1;
    }
    if (xmlStrcmp(curNode->name, BAD_CAST "root"))
    {
        fprintf(stderr,"document of the wrong type, root node != root");
        xmlFreeDoc(doc);
        return -1;
    }

    curNode = curNode->xmlChildrenNode;         // get first child
    while(curNode != NULL)
    {
        if(!xmlStrcmp(curNode->name, BAD_CAST "heart_beat"))
        {
            //heart beat handle
            curNode = curNode->children;
            while(curNode != NULL)
            {
                if(0 == xmlStrcmp(curNode->name,(const xmlChar *) "time"))
                {
                    char *content = (char *)xmlNodeListGetString(doc,curNode->xmlChildrenNode,1);
                    if(content)
                    {
                        printf("recv notify...\n");
                        xmlFree(content);
                    }
                     break;