我正在使用libxml2实际处理数据处理代码.我被困在一个无法移除的内存泄漏上.这是生成它的最小代码:
#include <stdlib.h>
#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <omp.h>
int main(void)
{
xmlDoc *doc;
int tn;
char fname[32];
omp_set_num_threads(2);
xmlInitParser();
#pragma omp parallel private(doc,tn,fname)
{
tn = omp_get_thread_num();
sprintf(fname,"testdoc%d.xml",tn);
doc = xmlReadFile(fname,NULL,0);
printf("document %s parsed on thread %d (%p)\n",fname,tn,doc);
xmlFreeDoc(doc);
}
xmlCleanupParser();
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
在运行时,输出为:
document testdoc0.xml parsed on thread 0 (0x1005413a0)
document testdoc1.xml parsed on thread 1 (0x1005543c0)
Run Code Online (Sandbox Code Playgroud)
确认我们确实有多线程,并且doc
在并行区域中确实是私有的.可以注意到我正确应用了libxml2(http://xmlsoft.org/threads.html)的线程安全说明.Valgrind报道:
HEAP SUMMARY:
in use at exit: 9,000 bytes in 8 blocks …
Run Code Online (Sandbox Code Playgroud)