0

I implemented one class to handle all zookeeper stuff. In connect(), I used m_zh = zookeeper_init( m_zkUrl.c_str(), NULL, 10000, 0, NULL, 0 ); to get a zookeeper handler. In the class destructor, I used zookeeper_close( m_zh ); to free this zookeeper handler. Why there still some memory leak? The output of valgrind is following.

...

==5003== 300 (60 direct, 240 indirect) bytes in 1 blocks are definitely lost in loss record 11 of 13

==5003== at 0x4C2CFA7: malloc (vg_replace_malloc.c:296)

==5003== by 0x596A594: nss_parse_service_list (nsswitch.c:678)

==5003== by 0x596B055: __nss_database_lookup (nsswitch.c:175)

==5003== by 0x6028623: ???

==5003== by 0x5923BFC: getpwuid_r@@GLIBC_2.2.5 (getXXbyYY_r.c:256)

==5003== by 0x5946E6E: __getlogin_r_loginuid (getlogin_r.c:68)

==5003== by 0x5946BC4: getlogin (getlogin.c:35)

==5003== by 0x4044D4: log_env (zookeeper.c:733)

==5003== by 0x406026: zookeeper_init (zookeeper.c:770)

==5003== by 0x402FFC: ZKConnection::connect() (zkconnection.cpp:37)

==5003== by 0x402962: main (test_zk.cpp:44)

...

==4624== LEAK SUMMARY:

==4624==    definitely lost: 60 bytes in 1 blocks

==4624==    indirectly lost: 240 bytes in 10 blocks

==4624==      possibly lost: 0 bytes in 0 blocks

==4624==    still reachable: 5,120 bytes in 2 blocks

==4624==         suppressed: 0 bytes in 0 blocks
BAE
  • 8,550
  • 22
  • 88
  • 171

1 Answers1

0

Are you sure that it is a memory leak?

Valgrind says that it's still reachable, which is generally not a problem.

The memory is allocated within getTSData, whose name suggests that it's using thread-local storage and whose implementation shows that it only allocates the buffer once per thread and properly registers the function freeBuffer to free this allocated memory when the thread ends.

Your code does have a memory leak (definitely lost: 60 bytes in 1 blocks), but I don't see anything to indicate that it's within zookeeper_init.

Josh Kelley
  • 56,064
  • 19
  • 146
  • 246
  • In my class, I commented all the statements except the two statements:**m_zh = zookeeper_init( m_zkUrl.c_str(), NULL, 10000, 0, NULL, 0 )** and **zookeeper_close( m_zh )** – BAE Mar 24 '15 at 19:30
  • @ChengchengPei - The Valgrind log snipped that you posted (`loss record 13 of 13`) is probably not a memory leak (because it's `still reachable`). zookeeper_init's calling calloc is probably not a memory leak, as I explained. Valgrind is saying you have a memory leak (`definitely lost: 60 bytes in 1 blocks`, `indirectly lost: 240 bytes in 10 blocks`), but it's presumably elsewhere in your code (outside of your `ZKConnection` class). – Josh Kelley Mar 24 '15 at 19:33
  • Sorry, I should posted loss record 11 0f 13. I already edited my question. But the functions, where problems are caused, are the same. – BAE Mar 24 '15 at 19:39
  • @ChengchengPei - There's nothing ZooKeeper-specific about `getlogin`. See http://stackoverflow.com/q/22932458/25507. – Josh Kelley Mar 24 '15 at 19:45