我编写了一个模块来定期将一些实时数据转储到 XML 文件中。我最终使用 xmlSaveFormatFileEnc() 来保存文件。
但是我通过 API xmlSaveFormatFileEnc() 得到了 Valgrind 的内存泄漏报告。
泄漏摘要如下,
==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 使用的新手。