C中GeoIP_new API可能存在内存泄漏

时间:2016-05-14 02:59:52

标签: c memory-leaks valgrind geoip

我在GeoIP 1.4.8和1.6.6上尝试了以下代码,它只释放了valgrind报告的40个分配中的5个分配。

根据这个post,我知道“仍然可以访问”,但我想确保在这里就是这种情况。对我来说这似乎很奇怪只能释放5个分配。有什么想法吗?

==1687== HEAP SUMMARY:
==1687==     in use at exit: 35,128 bytes in 35 blocks
==1687==   total heap usage: 40 allocs, 5 frees, 36,847 bytes allocated
==1687== 
==1687== 312 bytes in 1 blocks are still reachable in loss record 1 of 35
==1687==    at 0x4C2C947: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1687==    by 0x4E3DA5E: _GeoIP_setup_dbfilename (in /usr/lib/libGeoIP.so.1.6.6)
==1687==    by 0x4E3E789: GeoIP_new (in /usr/lib/libGeoIP.so.1.6.6)
==1687==    by 0x40075E: main (in /home/pixie/a.out)
==1687== 
==1687== 1,024 bytes in 1 blocks are still reachable in loss record 2 of 35
==1687==    at 0x4C2C947: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1687==    by 0x4E3D9A5: _GeoIP_full_path_to (in /usr/lib/libGeoIP.so.1.6.6)
==1687==    by 0x4E3DA70: _GeoIP_setup_dbfilename (in /usr/lib/libGeoIP.so.1.6.6)
==1687==    by 0x4E3E789: GeoIP_new (in /usr/lib/libGeoIP.so.1.6.6)
==1687==    by 0x40075E: main (in /home/pixie/a.out)
==1687== 
==1687== 1,024 bytes in 1 blocks are still reachable in loss record 3 of 35
==1687==    at 0x4C2C947: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1687==    by 0x4E3D9A5: _GeoIP_full_path_to (in /usr/lib/libGeoIP.so.1.6.6)
==1687==    by 0x4E3DA83: _GeoIP_setup_dbfilename (in /usr/lib/libGeoIP.so.1.6.6)
==1687==    by 0x4E3E789: GeoIP_new (in /usr/lib/libGeoIP.so.1.6.6)
==1687==    by 0x40075E: main (in /home/pixie/a.out)
==1687== 
==1687== 1,024 bytes in 1 blocks are still reachable in loss record 4 of 35
==1687==    at 0x4C2C947: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1687==    by 0x4E3D9A5: _GeoIP_full_path_to (in /usr/lib/libGeoIP.so.1.6.6)
==1687==    by 0x4E3DA96: _GeoIP_setup_dbfilename (in /usr/lib/libGeoIP.so.1.6.6)
==1687==    by 0x4E3E789: GeoIP_new (in /usr/lib/libGeoIP.so.1.6.6)
==1687==    by 0x40075E: main (in /home/pixie/a.out)
...

代码

#include <GeoIP.h>

int main (int argc, char **argv) {
  GeoIP * gp; 

  gp = GeoIP_new(GEOIP_STANDARD);
  printf("%s\n", GeoIP_country_code_by_addr(gp, "216.58.216.238"));
  GeoIP_delete(gp);

  return 0;
}

1 个答案:

答案 0 :(得分:1)

显然,以下call完成了工作(未记录)。

GeoIP_cleanup();