我正在尝试在我的应用程序中使用libssh,但我一直在泄漏内存。我正在尝试的最简单的代码是:
#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());
按预期工作,但Valgrind抱怨泄漏(好像没有释放ssh_connect分配的所有内存):
==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
我正在使用:
我做错了什么?任何帮助将不胜感激。
答案 0 :(得分:2)
你没有做错任何事。 libssh中没有内存泄漏,这是你的libc:)
答案 1 :(得分:2)
这是你的罪魁祸首
==17385== by 0x60A6BFC: getpwuid_r@@GLIBC_2.2.5 (getXXbyYY_r.c:256)
我之前已经追了这个,我只是告诉你,GLIBC getpw *和getgr *函数会分配少量内存,而这些内存永远不会被释放。开发商表示他们不会修复它。我发现在某个地方的电子邮件帖子中却无法找到参考文件。
将此添加到您的抑制文件并继续前进。