繁体   English   中英

xmlStrndup中的Valgrind内存泄漏

[英]Valgrind memory leak in xmlStrndup

我试图通过遍历元素来解析xml文件。

这是我将要调用的基本方法。 但是,每当调用此方法时,我都会观察到泄漏。

码:

std::string getUrl(std::vector<std::string> keyPath, std::string element, std::string fName)
{

    xmlDocPtr m_doc;
    xmlNodePtr m_cur;
    std::string m_fileName;
    bool isEmpty;
    int i=0;
    std::string value = "";
    isEmpty = false;
    m_fileName = fName;
    struct stat stat_buf;
    int rc = stat(m_fileName.c_str(), &stat_buf);
    if(rc==0)
    {
        m_doc = xmlParseFile(m_fileName.c_str());
    }
    else
    {
        isEmpty = true;
    }

    if(isEmpty)
    {
       value = "Empty file found";
       xmlFreeDoc(m_doc);
       xmlCleanupCharEncodingHandlers();
       xmlCleanupParser();
       return value;
    }
    if(m_doc != NULL)
    {
        m_cur = xmlDocGetRootElement(m_doc);
    }
    if(m_cur != NULL)
    {
        if(!xmlStrcmp(m_cur->name, (const xmlChar *)(keyPath.at(i).c_str())))
        {
            m_cur = m_cur->xmlChildrenNode;
            ++i;
        }
        else
        {
            value = "root element not found";
            return value;
        }
        while(m_cur != NULL)
        {
            if (!xmlStrcmp ( m_cur -> name, ( const xmlChar * ) keyPath.at(i).c_str()))
            {
                m_cur = m_cur->xmlChildrenNode;
                i++;
            }
            m_cur = m_cur -> next;
            if (!xmlStrcmp ( m_cur -> name, ( const xmlChar * ) keyPath.back().c_str()))
            {
                m_cur = m_cur->xmlChildrenNode;
                break;
            }
        }
        while (m_cur != NULL)
        {
        if (!xmlStrcmp ( m_cur -> name, ( const xmlChar * ) element.c_str()))
        {
            if(xmlNodeGetContent(m_cur->xmlChildrenNode) != NULL)
            value = (char*)(xmlNodeGetContent(m_cur->xmlChildrenNode));
            else
            value = "";
        }
        m_cur = m_cur -> next;
        }
    }

    //call the necessary cleanup APIs of libxml2 to free the dynamically allocated memory
    xmlFreeDoc(m_doc);
    xmlCleanupCharEncodingHandlers();
    xmlCleanupParser();

    if(!value.empty())
    {
        return value;
    }
    else
    {
        value = "value not found";
        return value;
    }
}

Valgrind泄漏如下:

==1598== 
==1598== HEAP SUMMARY:
==1598==     in use at exit: 139,402 bytes in 1,053 blocks
==1598==   total heap usage: 1,222 allocs, 169 frees, 284,997 bytes allocated
==1598== 
==1598== 28 bytes in 1 blocks are definitely lost in loss record 29 of 80
==1598==    at 0x4C2A6FE: malloc (vg_replace_malloc.c:296)
==1598==    by 0x526D928: xmlStrndup (in /usr/lib64/libxml2.so.2.9.1)
==1598==    by 0x40198D: getUrl(std::vector<std::string, std::allocator<std::string> >, std::string, std::string) (in /cluster/home/XmlParser)
==1598==    by 0x401BAE: parseXmlFile(std::vector<std::string, std::allocator<std::string> >, std::string, std::string) (in /cluster/home/XmlParser)
==1598==    by 0x402A56: main (in /cluster/home/XmlParser)
==1598== 
==1598== 28 bytes in 1 blocks are definitely lost in loss record 30 of 80
==1598==    at 0x4C2A6FE: malloc (vg_replace_malloc.c:296)
==1598==    by 0x526D928: xmlStrndup (in /usr/lib64/libxml2.so.2.9.1)
==1598==    by 0x4019A4: getUrl(std::vector<std::string, std::allocator<std::string> >, std::string, std::string) (in /cluster/home/XmlParser)
==1598==    by 0x401BAE: parseXmlFile(std::vector<std::string, std::allocator<std::string> >, std::string, std::string) (in /cluster/home/XmlParser)
==1598==    by 0x402A56: main (in /cluster/home/XmlParser)
==1598== 
==1598== 30 bytes in 1 blocks are definitely lost in loss record 31 of 80
==1598==    at 0x4C2A6FE: malloc (vg_replace_malloc.c:296)
==1598==    by 0x526D928: xmlStrndup (in /usr/lib64/libxml2.so.2.9.1)
==1598==    by 0x40198D: getUrl(std::vector<std::string, std::allocator<std::string> >, std::string, std::string) (in /cluster/home/XmlParser)
==1598==    by 0x401BAE: parseXmlFile(std::vector<std::string, std::allocator<std::string> >, std::string, std::string) (in /cluster/home/XmlParser)
==1598==    by 0x402CD2: main (in /cluster/home/XmlParser)
==1598== 
==1598== 30 bytes in 1 blocks are definitely lost in loss record 32 of 80
==1598==    at 0x4C2A6FE: malloc (vg_replace_malloc.c:296)
==1598==    by 0x526D928: xmlStrndup (in /usr/lib64/libxml2.so.2.9.1)
==1598==    by 0x4019A4: getUrl(std::vector<std::string, std::allocator<std::string> >, std::string, std::string) (in /cluster/home/XmlParser)
==1598==    by 0x401BAE: parseXmlFile(std::vector<std::string, std::allocator<std::string> >, std::string, std::string) (in /cluster/home/XmlParser)
==1598==    by 0x402CD2: main (in /cluster/home/XmlParser)
==1598== 
==1598== 30 bytes in 1 blocks are definitely lost in loss record 33 of 80
==1598==    at 0x4C2A6FE: malloc (vg_replace_malloc.c:296)
==1598==    by 0x526D928: xmlStrndup (in /usr/lib64/libxml2.so.2.9.1)
==1598==    by 0x40198D: getUrl(std::vector<std::string, std::allocator<std::string> >, std::string, std::string) (in /cluster/home/XmlParser)
==1598==    by 0x401FF4: parseXmlFile(std::vector<std::string, std::allocator<std::string> >, std::string, std::string) (in /cluster/home/XmlParser)
==1598==    by 0x402F30: main (in /cluster/home/XmlParser)
==1598== 
==1598== 30 bytes in 1 blocks are definitely lost in loss record 34 of 80
==1598==    at 0x4C2A6FE: malloc (vg_replace_malloc.c:296)
==1598==    by 0x526D928: xmlStrndup (in /usr/lib64/libxml2.so.2.9.1)
==1598==    by 0x4019A4: getUrl(std::vector<std::string, std::allocator<std::string> >, std::string, std::string) (in /cluster/home/XmlParser)
==1598==    by 0x401FF4: parseXmlFile(std::vector<std::string, std::allocator<std::string> >, std::string, std::string) (in /cluster/home/XmlParser)
==1598==    by 0x402F30: main (in /cluster/home/XmlParser)
==1598== 
==1598== 21,852 (176 direct, 21,676 indirect) bytes in 1 blocks are definitely lost in loss record 77 of 80
==1598==    at 0x4C2A6FE: malloc (vg_replace_malloc.c:296)
==1598==    by 0x5215324: xmlNewDoc (in /usr/lib64/libxml2.so.2.9.1)
==1598==    by 0x52C01B8: xmlSAX2StartDocument (in /usr/lib64/libxml2.so.2.9.1)
==1598==    by 0x520E25D: xmlParseDocument (in /usr/lib64/libxml2.so.2.9.1)
==1598==    by 0x520E561: xmlSAXParseFileWithData (in /usr/lib64/libxml2.so.2.9.1)
==1598==    by 0x40181A: getUrl(std::vector<std::string, std::allocator<std::string> >, std::string, std::string) (in /cluster/home/XmlParser)
==1598==    by 0x401BAE: parseXmlFile(std::vector<std::string, std::allocator<std::string> >, std::string, std::string) (in /cluster/home/XmlParser)
==1598==    by 0x402A56: main (in /cluster/home/XmlParser)
==1598== 
==1598== 21,852 (176 direct, 21,676 indirect) bytes in 1 blocks are definitely lost in loss record 78 of 80
==1598==    at 0x4C2A6FE: malloc (vg_replace_malloc.c:296)
==1598==    by 0x5215324: xmlNewDoc (in /usr/lib64/libxml2.so.2.9.1)
==1598==    by 0x52C01B8: xmlSAX2StartDocument (in /usr/lib64/libxml2.so.2.9.1)
==1598==    by 0x520E25D: xmlParseDocument (in /usr/lib64/libxml2.so.2.9.1)
==1598==    by 0x520E561: xmlSAXParseFileWithData (in /usr/lib64/libxml2.so.2.9.1)
==1598==    by 0x40181A: getUrl(std::vector<std::string, std::allocator<std::string> >, std::string, std::string) (in /cluster/home/XmlParser)
==1598==    by 0x401BAE: parseXmlFile(std::vector<std::string, std::allocator<std::string> >, std::string, std::string) (in /cluster/home/XmlParser)
==1598==    by 0x402CD2: main (in /cluster/home/XmlParser)
==1598== 
==1598== 21,852 (176 direct, 21,676 indirect) bytes in 1 blocks are definitely lost in loss record 79 of 80
==1598==    at 0x4C2A6FE: malloc (vg_replace_malloc.c:296)
==1598==    by 0x5215324: xmlNewDoc (in /usr/lib64/libxml2.so.2.9.1)
==1598==    by 0x52C01B8: xmlSAX2StartDocument (in /usr/lib64/libxml2.so.2.9.1)
==1598==    by 0x520E25D: xmlParseDocument (in /usr/lib64/libxml2.so.2.9.1)
==1598==    by 0x520E561: xmlSAXParseFileWithData (in /usr/lib64/libxml2.so.2.9.1)
==1598==    by 0x40181A: getUrl(std::vector<std::string, std::allocator<std::string> >, std::string, std::string) (in /cluster/home/XmlParser)
==1598==    by 0x401FF4: parseXmlFile(std::vector<std::string, std::allocator<std::string> >, std::string, std::string) (in /cluster/home/XmlParser)
==1598==    by 0x402F30: main (in /cluster/home/XmlParser)
==1598== 
==1598== LEAK SUMMARY:
==1598==    definitely lost: 704 bytes in 9 blocks
==1598==    indirectly lost: 65,028 bytes in 1,023 blocks
==1598==      possibly lost: 0 bytes in 0 blocks
==1598==    still reachable: 73,670 bytes in 21 blocks
==1598==         suppressed: 0 bytes in 0 blocks
==1598== Reachable blocks (those to which a pointer was found) are not shown.
==1598== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==1598== 
==1598== For counts of detected and suppressed errors, rerun with: -v
==1598== ERROR SUMMARY: 9 errors from 9 contexts (suppressed: 0 from 0)

如果您能帮助我真正造成泄漏的事情,我将不胜感激。 提前致谢

这段代码导致了两次内存泄漏:

if(xmlNodeGetContent(m_cur->xmlChildrenNode) != NULL)
    value = (char*)(xmlNodeGetContent(m_cur->xmlChildrenNode));
else
    value = "";

xmlNodeGetContent返回必须使用xmlFree释放的字符串。 因此,代码应如下所示:

xmlChar *content = xmlNodeGetContent(m_cur->xmlChildrenNode);
if (content != NULL) {
    value = (char*)content;
    xmlFree(content);
}
else {
    value = "";
}

您有时还会泄漏xmlDoc 这可能是由于无法释放文档的“找不到根元素”返回路径引起的。

现在,nwellnhof已为您解答了泄漏点所在的问题。

我会尽力告诉您如何修复它们。 注意未经测试的代码。

我们在这里(st-)使用std::unique_ptr作为scope_guard。

首先创建一个通用的获取字符串方法,该方法可以解决无字符串问题。

std::string xmlGetString(xmlNodePtr m_cur) {
  std::unique_ptr<xmlChar> 
    content(xmlNodeGetContent(m_cur->xmlChildrenNode), xmlFree); // 1

  if (content != NULL) {
    return (char*)content;
  } else {
    return "";
  }
}

注意1:xmlFree是std::unique_ptr的删除器

如果xmlFree的答案不为NULL,则必须在调用xmlFree之前将其打包到另一个检查NULL的方法中。

(这看起来效率很低,xmlNodeGetContent复制了一些内部数据,然后以std::string返回,需要文本的另一个副本,至少可以省略字符串到调用方中左值的最终副本。 )

xmlFreeDoc相同的技巧,首先进行一些重写

if (rc != 0) {
  // any other xml clean up needed???
  return "Empty file found";
}

然后将其声明为

std::unique_ptr<xmlDoc> m_doc(xmlParseFile(m_fileName.c_str()), Cleanup); 
// note the ptr is gone to the unique_ptr

和其他方法

void Cleanup(xmlDocPtr m_doc) {
   xmlFreeDoc(m_doc);
   xmlCleanupCharEncodingHandlers();
   xmlCleanupParser();
}

然后,当m_doc离开范围时,将调用Cleanup

现在您需要清理,以免做不必要的工作。

您应该将所有其他xml函数打包到某些c ++中,或者更改xml库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM