You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用libssh的ssh_connect()函数出现内存泄漏问题求助

libssh ssh_connect() 内存泄漏问题求助

问题概述

使用libssh库的ssh_connect()函数时出现内存泄漏,通过Valgrind命令valgrind --leak-check=full ./script检测发现:

  • 单次调用memorytestssh()函数,程序退出时丢失512字节内存;
  • 循环调用20次时,丢失10240字节内存,泄漏量随调用次数线性增长;
  • 仅当ssh_connect()返回SSH_OK(成功连接到TCP 22端口开放的设备)时才会触发泄漏。

已尝试查阅官方文档、升级/降级libssh版本,均未解决问题,现寻求帮助排查泄漏原因及解决方法。

Valgrind检测输出

非循环调用memorytestssh()时的输出

==43309==
==43309== HEAP SUMMARY:
==43309==     in use at exit: 512 bytes in 8 blocks
==43309==   total heap usage: 451 allocs, 443 frees, 104,640 bytes allocated
==43309==
==43309== 512 (80 direct, 432 indirect) bytes in 1 blocks are definitely lost in loss record 8 of 8
==43309==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==43309==    by 0x4C4FC0D: CRYPTO_zalloc (in /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1)
==43309==    by 0x4C49023: ??? (in /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1)
==43309==    by 0x486E38F: ??? (in /usr/lib/x86_64-linux-gnu/libssh.so.4.8.4)
==43309==    by 0x486E70C: ??? (in /usr/lib/x86_64-linux-gnu/libssh.so.4.8.4)
==43309==    by 0x487D9C7: ??? (in /usr/lib/x86_64-linux-gnu/libssh.so.4.8.4)
==43309==    by 0x487E0EB: ??? (in /usr/lib/x86_64-linux-gnu/libssh.so.4.8.4)
==43309==    by 0x4888197: ??? (in /usr/lib/x86_64-linux-gnu/libssh.so.4.8.4)
==43309==    by 0x48845B2: ??? (in /usr/lib/x86_64-linux-gnu/libssh.so.4.8.4)
==43309==    by 0x48855EF: ??? (in /usr/lib/x86_64-linux-gnu/libssh.so.4.8.4)
==43309==    by 0x48856BA: ??? (in /usr/lib/x86_64-linux-gnu/libssh.so.4.8.4)
==43309==    by 0x486B39F: ssh_connect (in /usr/lib/x86_64-linux-gnu/libssh.so.4.8.4)
==43309==
==43309== LEAK SUMMARY:
==43309==    definitely lost: 80 bytes in 1 blocks
==43309==    indirectly lost: 432 bytes in 7 blocks
==43309==      possibly lost: 0 bytes in 0 blocks
==43309==    still reachable: 0 bytes in 0 blocks
==43309==         suppressed: 0 bytes in 0 blocks
==43309==
==43309== For lists of detected and suppressed errors, rerun with: -s
==43309== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

循环调用20次memorytestssh()时的输出

==55457==
==55457== HEAP SUMMARY:
==55457==     in use at exit: 10,240 bytes in 160 blocks
==55457==   total heap usage: 7,177 allocs, 7,017 frees, 1,743,370 bytes allocated
==55457==
==55457== 10,240 (1,600 direct, 8,640 indirect) bytes in 20 blocks are definitely lost in loss record 8 of 8
==55457==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==55457==    by 0x4C4FC0D: CRYPTO_zalloc (in /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1)
==55457==    by 0x4C49023: ??? (in /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1)
==55457==    by 0x486E38F: ??? (in /usr/lib/x86_64-linux-gnu/libssh.so.4.8.4)
==55457==    by 0x486E70C: ??? (in /usr/lib/x86_64-linux-gnu/libssh.so.4.8.4)
==55457==    by 0x487D9C7: ??? (in /usr/lib/x86_64-linux-gnu/libssh.so.4.8.4)
==55457==    by 0x487E0EB: ??? (in /usr/lib/x86_64-linux-gnu/libssh.so.4.8.4)
==55457==    by 0x4888197: ??? (in /usr/lib/x86_64-linux-gnu/libssh.so.4.8.4)
==55457==    by 0x48845B2: ??? (in /usr/lib/x86_64-linux-gnu/libssh.so.4.8.4)
==55457==    by 0x48855EF: ??? (in /usr/lib/x86_64-linux-gnu/libssh.so.4.8.4)
==55457==    by 0x48856BA: ??? (in /usr/lib/x86_64-linux-gnu/libssh.so.4.8.4)
==55457==    by 0x486B39F: ssh_connect (in /usr/lib/x86_64-linux-gnu/libssh.so.4.8.4)
==55457==
==55457== LEAK SUMMARY:
==55457==    definitely lost: 1,600 bytes in 20 blocks
==55457==    indirectly lost: 8,640 bytes in 140 blocks
==55457==      possibly lost: 0 bytes in 0 blocks
==55457==    still reachable: 0 bytes in 0 blocks
==55457==         suppressed: 0 bytes in 0 blocks
==55457==
==55457== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

复现代码

#include <stdio.h>
#include <string.h>
#include <libssh/libssh.h>

void memorytestssh()
{
    char * username, * password;
    ssh_session session = ssh_new();
    if (session == NULL)
    {
        printf("cant alloc\n");
        return;
    }
    
    int port = 22;

    char * serversss = "1.1.1.1";
    char * test = "root:somepassword";
    char userpassword[256];
    strcpy(userpassword, test);
    char * save_ptr;

    username = strtok_r(userpassword, ":", &save_ptr); // 实际项目是多线程环境(用libpthread),所以用strtok_r
    password = strtok_r(NULL, ":", &save_ptr);

    ssh_options_set(session, SSH_OPTIONS_HOST, serversss);
    ssh_options_set(session, SSH_OPTIONS_USER, username);
    ssh_options_set(session, SSH_OPTIONS_PORT, &port);

    if(ssh_connect(session) != SSH_OK)
    {
        printf("cant connec\n");
        ssh_free(session);
        return;
    }
    printf("connected\n");
    if (ssh_userauth_password(session, NULL, password) != SSH_AUTH_SUCCESS)
    {
        printf("auth not work\n");
        ssh_disconnect(session);
        ssh_free(session);
        return;
    }
    
    // print oh yes worked
    ssh_disconnect(session);
    ssh_free(session);
}

int main(void)
{
    for (size_t i = 0; i < 20; i++)
    {
        printf("ran %ld time/s\n", i + 1);
        memorytestssh();
    }
}

内容的提问来源于stack exchange,提问作者coolcoolyes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 00:55:57