【问题标题】:Memory-leaks when using libssh使用 libssh 时的内存泄漏
【发布时间】:2013-03-08 02:50:55
【问题描述】:

我正在尝试在我的应用程序中使用 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

我正在使用:

  • Ubuntu 12.04 x64 LTS
  • libssh 版本0.5.2-1ubuntu0.12.04.1(目前最新可用)
  • g++/gcc 版本 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
  • valgrind-3.7.0

我做错了什么?任何帮助将不胜感激。

【问题讨论】:

    标签: ubuntu memory-leaks g++ valgrind libssh


    【解决方案1】:

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

    【讨论】:

    • 我觉得这篇文章确实提供了答案。因为问题是:“我做错了什么?”。
    【解决方案2】:

    这是你的罪魁祸首

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

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

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

    【讨论】:

      猜你喜欢
      • 2021-07-06
      • 2013-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-17
      相关资源
      最近更新 更多