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.