0

I am trying to use libssh in my application, but I keep getting memory leaks. The most simplistic code, I am trying, is:

#include <libssh/libssh.h>
#define SSH_NO_CPP_EXCEPTIONS
assert(0 == ssh_init());

ssh_session m_session;
assert(m_session = ssh_new());

int port = 22;
assert(0 == ssh_options_set(m_session, SSH_OPTIONS_HOST, "user1@computer1"));
assert(0 == ssh_options_set(m_session, SSH_OPTIONS_PORT, &port));
assert(SSH_OK == ssh_connect(m_session));

ssh_disconnect(m_session);
ssh_free(m_session);
assert(0 == ssh_finalize());

Works as expected, but Valgrind complaints about leaks (as if all memory allocated by ssh_connect was not freed):

==17385== HEAP SUMMARY:
==17385==     in use at exit: 300 bytes in 11 blocks
==17385==   total heap usage: 2,008 allocs, 1,997 frees, 5,529,273 bytes allocated
==17385== 
==17385== 300 (60 direct, 240 indirect) bytes in 1 blocks are definitely lost in loss record 11 of 11
==17385==    at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==17385==    by 0x60ED594: nss_parse_service_list (nsswitch.c:678)
==17385==    by 0x60EE055: __nss_database_lookup (nsswitch.c:175)
==17385==    by 0x94AC623: ???
==17385==    by 0x60A6BFC: getpwuid_r@@GLIBC_2.2.5 (getXXbyYY_r.c:256)
==17385==    by 0x58B25F7: ssh_get_user_home_dir (misc.c:216)
==17385==    by 0x58B2F42: ssh_path_expand_tilde (misc.c:673)
==17385==    by 0x58B3F57: ssh_options_set (options.c:457)
==17385==    by 0x58B4702: ssh_options_apply (options.c:915)
==17385==    by 0x58A72CD: ssh_connect (client.c:652)
==17385==    by 0x421896: sshTest() (main.cpp:589)
==17385==    by 0x418F67: main (main.cpp:51)
==17385== 
==17385== LEAK SUMMARY:
==17385==    definitely lost: 60 bytes in 1 blocks
==17385==    indirectly lost: 240 bytes in 10 blocks
==17385==      possibly lost: 0 bytes in 0 blocks
==17385==    still reachable: 0 bytes in 0 blocks
==17385==         suppressed: 0 bytes in 0 blocks

I am using:

  • Ubuntu 12.04 x64 LTS
  • libssh version 0.5.2-1ubuntu0.12.04.1 (latest available at the moment)
  • g++/gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
  • valgrind-3.7.0

What am I doing wrong? Any help would be greatly appreciated.

4

2 回答 2

2

你没有做错任何事。libssh 中没有内存泄漏,这是您的 libc :)

于 2012-12-13T18:32:40.587 回答
2

这是你的罪魁祸首

==17385==    by 0x60A6BFC: getpwuid_r@@GLIBC_2.2.5 (getXXbyYY_r.c:256)

我以前追过这个,我只想告诉你,GLIBC getpw* 和 getgr* 函数分配少量的内存,这些内存永远不会被释放。开发人员表示他们不会修复它。我在某个地方的电子邮件线程中发现了这一点,但无法立即找到参考。

将此添加到您的抑制文件中并继续前进。

于 2013-03-07T18:50:55.837 回答