2

以下代码正确编译并且 valgrind 报告没有泄漏:

# include <libxml/encoding.h>
# include <libxml/xmlwriter.h>

int main(void) {
  xmlTextWriterPtr XMLWriter = xmlNewTextWriterFilename("example.xml", 0);
  xmlTextWriterStartDocument(XMLWriter, NULL, NULL, NULL);
  xmlTextWriterEndDocument(XMLWriter);
  xmlFreeTextWriter(XMLWriter);
  xmlCleanupParser();
  return 0;
}

-----------------------------------------

==26059== HEAP SUMMARY:
==26059==     in use at exit: 0 bytes in 0 blocks
==26059==   total heap usage: 16 allocs, 16 frees, 4,774 bytes allocated
==26059== 
==26059== All heap blocks were freed -- no leaks are possible

但是,一旦在 xmlNewTextWriterFilename 调用中指定了编码,就会出现希拉里:

# include <libxml/encoding.h>
# include <libxml/xmlwriter.h>

int main(void) {
  xmlTextWriterPtr XMLWriter = xmlNewTextWriterFilename("example.xml", 0);
  xmlTextWriterStartDocument(XMLWriter, NULL, "UTF-8", NULL);
  xmlTextWriterEndDocument(XMLWriter);
  xmlFreeTextWriter(XMLWriter);
  xmlCleanupParser();
  return 0;
}

-----------------------------------------

==26082== HEAP SUMMARY:
==26082==     in use at exit: 422 bytes in 17 blocks
==26082==   total heap usage: 36 allocs, 19 frees, 9,224 bytes allocated
==26082== 
==26082== 5 bytes in 1 blocks are still reachable in loss record 1 of 17
==26082==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==26082==    by 0x40EC23B: xmlStrndup (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==26082==    by 0x494942FF: ???
==26082== 
==26082== 6 bytes in 1 blocks are still reachable in loss record 2 of 17
==26082==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==26082==    by 0x40EC23B: xmlStrndup (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==26082==    by 0x37: ???
==26082== 
==26082== 6 bytes in 1 blocks are still reachable in loss record 3 of 17
==26082==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==26082==    by 0x40EC23B: xmlStrndup (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==26082==    by 0x39350048: ???
==26082== 
==26082== 7 bytes in 1 blocks are still reachable in loss record 4 of 17
==26082==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==26082==    by 0x40EC23B: xmlStrndup (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==26082==    by 0x45003630: ???
==26082== 
==26082== 9 bytes in 1 blocks are still reachable in loss record 5 of 17
==26082==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==26082==    by 0x40EC23B: xmlStrndup (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==26082==    by 0x454C3630: ???
==26082== 
==26082== 9 bytes in 1 blocks are still reachable in loss record 6 of 17
==26082==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==26082==    by 0x40EC23B: xmlStrndup (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==26082==    by 0x45423630: ???
==26082== 
==26082== 9 bytes in 1 blocks are still reachable in loss record 7 of 17
==26082==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==26082==    by 0x40EC23B: xmlStrndup (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==26082==    by 0x49494352: ???
==26082== 
==26082== 11 bytes in 1 blocks are still reachable in loss record 8 of 17
==26082==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==26082==    by 0x40EC23B: xmlStrndup (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==26082==    by 0x39353837: ???
==26082== 
==26082== 20 bytes in 1 blocks are still reachable in loss record 9 of 17
==26082==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==26082==    by 0x406E9D1: xmlNewCharEncodingHandler (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==26082==    by 0x406EB28: xmlInitCharEncodingHandlers (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==26082==    by 0x41B54D2: (below main) (libc-start.c:226)
==26082== 
==26082== 20 bytes in 1 blocks are still reachable in loss record 10 of 17
==26082==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==26082==    by 0x406E9D1: xmlNewCharEncodingHandler (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==26082==    by 0x406EB44: xmlInitCharEncodingHandlers (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==26082==    by 0x41B54D2: (below main) (libc-start.c:226)
==26082== 
==26082== 20 bytes in 1 blocks are still reachable in loss record 11 of 17
==26082==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==26082==    by 0x406E9D1: xmlNewCharEncodingHandler (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==26082==    by 0x406EB6C: xmlInitCharEncodingHandlers (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==26082==    by 0x41B54D2: (below main) (libc-start.c:226)
==26082== 
==26082== 20 bytes in 1 blocks are still reachable in loss record 12 of 17
==26082==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==26082==    by 0x406E9D1: xmlNewCharEncodingHandler (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==26082==    by 0x406EB94: xmlInitCharEncodingHandlers (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==26082==    by 0x41B54D2: (below main) (libc-start.c:226)
==26082== 
==26082== 20 bytes in 1 blocks are still reachable in loss record 13 of 17
==26082==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==26082==    by 0x406E9D1: xmlNewCharEncodingHandler (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==26082==    by 0x406EBB6: xmlInitCharEncodingHandlers (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==26082==    by 0x41B54D2: (below main) (libc-start.c:226)
==26082== 
==26082== 20 bytes in 1 blocks are still reachable in loss record 14 of 17
==26082==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==26082==    by 0x406E9D1: xmlNewCharEncodingHandler (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==26082==    by 0x406EBCC: xmlInitCharEncodingHandlers (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==26082==    by 0x41B54D2: (below main) (libc-start.c:226)
==26082== 
==26082== 20 bytes in 1 blocks are still reachable in loss record 15 of 17
==26082==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==26082==    by 0x406E9D1: xmlNewCharEncodingHandler (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==26082==    by 0x406EBE2: xmlInitCharEncodingHandlers (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==26082==    by 0x41B54D2: (below main) (libc-start.c:226)
==26082== 
==26082== 20 bytes in 1 blocks are still reachable in loss record 16 of 17
==26082==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==26082==    by 0x406E9D1: xmlNewCharEncodingHandler (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==26082==    by 0x406EC02: xmlInitCharEncodingHandlers (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==26082==    by 0x41B54D2: (below main) (libc-start.c:226)
==26082== 
==26082== 200 bytes in 1 blocks are still reachable in loss record 17 of 17
==26082==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==26082==    by 0x406EABE: xmlInitCharEncodingHandlers (in /usr/lib/i386-linux-gnu/libxml2.so.2.7.8)
==26082==    by 0x41B54D2: (below main) (libc-start.c:226)
==26082== 
==26082== LEAK SUMMARY:
==26082==    definitely lost: 0 bytes in 0 blocks
==26082==    indirectly lost: 0 bytes in 0 blocks
==26082==      possibly lost: 0 bytes in 0 blocks
==26082==    still reachable: 422 bytes in 17 blocks
==26082==         suppressed: 0 bytes in 0 blocks

做什么?这是家庭作业 - 不能忽视 valgrind 结果。

4

1 回答 1

5

在调用xmlCleanupParser()之前添加对xmlCleanupCharEncodingHandlers() 的调用。

在我的 Mac OS 10.7.5 开发机器(libxml 20703)上,这修复了 libxml2 中报告的“仍然可以访问”的泄漏。

顺便一提, ???表示目标文件的调试符号不可用。在跟踪内存泄漏以获取此信息时非常有帮助。安装调试信息的过程取决于 Linux 发行版。例如,在 Ubuntu 上,您可以使用sudo apt-get install libxml2-dbg. 请参阅https://live.gnome.org/GettingTraces/DistroSpecificInstructions

于 2012-12-16T18:20:49.590 回答