使用 libxml2 的 xmlSaveFormatFileEnc 中的 valgrind 导致 XML 内存泄漏

标签 xml memory-leaks valgrind libxml2

我已经编写了一个模块来将一些实时数据转储到 XML 文件中 有规律的间隔。 我最终使用 xmlSaveFormatFileEnc( ) 来保存文件。

但是我通过 Valgrind 和 API 收到内存泄漏报告 xmlSaveFormatFileEnc( ).

泄漏摘要如下,

==8355== 261,507,768 bytes in 506,798 blocks are definitely lost in los s
record 109 of 109
==8355==    at 0x402BE68: malloc (in
/usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==8355==    by 0x40E43FB: xmlGetGlobalState (in
/usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==8355==    by 0x40E3A22: __xmlIndentTreeOutput (in
/usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==8355==    by 0x414BE54: ??? (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==8355==    by 0x414C598: ??? (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==8355==    by 0x414BE42: ??? (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==8355==    by 0x414C598: ??? (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==8355==    by 0x414BE42: ??? (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==8355==    by 0x414C598: ??? (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==8355==    by 0x414BE42: ??? (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==8355==    by 0x414C598: ??? (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==8355==    by 0x414BE42: ??? (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==8355==    by 0x414C598: ??? (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==8355==    by 0x414BE42: ??? (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==8355==    by 0x414B8B7: ??? (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==8355==    by 0x414D339: xmlSaveFormatFileEnc (in
/usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==8355==    by 0x805D5F0: store_to_xmlfile(char*, char*)
(ram_utilities.cpp:248)
==8355==    by 0x805D3E1: FetchDataFromFifo() (ram_utilities.cpp:198)
==8355==    by 0x8050A5D: ThreadFn_RqstAnlysr(void*)
(request_analyser_module.cpp:134)
==8355==    by 0x42FDD4B: start_thread (pthread_create.c:308)
==8355==

请建议我避免内存泄漏的措施。

我还在我的代码中添加了 xmlinitparser() 和 xmlCleanupParser(),但内存泄漏并没有停止。

存储到xml的代码如下...

void addxmldata(xmlDocPtr , xmlNodePtr , struct Res_Am_Snr_Body *);
void add_hr_dt(xmlNodePtr , struct Res_Am_Snr_Body *);
void add_min_dt(xmlNodePtr , struct Res_Am_Snr_Body *);
void add_snr_data(xmlNodePtr , struct Res_Am_Snr_Body *);

int store_to_xmlfile(char* msgbuf, char* xmlfile)
{
        int ret_val;
        xmlDocPtr doc = NULL;
        xmlNodePtr root_node = NULL;

        xmlInitParser();
        printf("In store_to_xmlfilexml file--->%s\n", xmlfile);

        doc = xmlParseFile(xmlfile);

        if(doc == NULL)
        {
                cout<<"failed to open-->"<<xmlfile<<endl;
        }

        root_node = xmlDocGetRootElement(doc);

        addxmldata(doc, root_node, msgbuf);

        cout<<"in store_to_xmlfile return"<<endl;

        ret_val = xmlSaveFormatFileEnc(xmlfile, doc, "UTF-8", 1);

        xmlFreeDoc(doc);

        xmlCleanupParser();

        return ret_val;

我正在调用 addxmldata,后者会在特定的时间间隔内依次调用 addhr、min 和 snr。

xml 文件构建完美,但存在大量内存泄漏。我的程序应该连续运行,但在一个半小时后崩溃。 valgrind 中的内存泄漏几乎是 2 GB。 valgrind 报告显示重复输入上述错误。

请帮助我,因为我是 valgrind 用法的新手。

最佳答案

尝试移除对 xmlCleanupParser 的调用。 documentation说:

This function name is somewhat misleading. It does not clean up parser state, it cleans up memory allocated by the library itself. It is a cleanup function for the XML library. It tries to reclaim all related global memory allocated for the library processing. It doesn't deallocate any document related memory. One should call xmlCleanupParser() only when the process has finished using the library and all XML/HTML documents built with it. See also xmlInitParser() which has the opposite function of preparing the library for operations. WARNING: if your application is multithreaded or has plugin support calling this may crash the application if another thread or a plugin is still using libxml2. It's sometimes very hard to guess if libxml2 is in use in the application, some libraries or plugins may use it without notice. In case of doubt abstain from calling this function or do it just before calling exit() to avoid leak reports from valgrind !

注意最后一句话。 xmlCleanupParser 只应在退出进程之前立即调用。

关于使用 libxml2 的 xmlSaveFormatFileEnc 中的 valgrind 导致 XML 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18307146/

相关文章:

xml - 将 tz 偏移量内不带冒号的 ISO 日期字符串转换为 XMLGregorianCalendar

java - 对 WeakReference.get() 对象的硬引用会导致内存泄漏吗?

c - Valgrind 声称存在未释放的内存。这很糟糕吗?

c++ - 解决未指向我的代码的 valgrind 内存错误

c - Valgrind 显示调用 pr_set_ptracer 时出错,vgdb 可能会阻塞

java - 如何在 JBoss 应用服务器中创建数据源

xml - 在 Flex 中将分层数据转换为 XML

Listview 顶部的 Android Logo

objective-c - CIContext内存泄漏

objective-c - 他们为什么要这样初始化指针?