- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试一个非常简单的 libssh
实现,但 valgrind 显示内存泄漏。
代码是:
#include <libssh/libssh.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main()
{
ssh_session my_ssh_session = ssh_new();
int port = 22;
int rc, state;
const char *ip = "192.168.125.241";
char *password;
if (my_ssh_session == NULL)
exit(-1);
ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, ip);
ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
rc = ssh_connect(my_ssh_session);
if (rc != SSH_OK)
{
fprintf(stderr, "Error connecting to localhost: %s\n",
ssh_get_error(my_ssh_session));
exit(-1);
}
state = ssh_is_server_known(my_ssh_session);
if(state == SSH_SERVER_KNOWN_OK)
printf("already added\n");
else
printf("require addition\n");
rc = ssh_userauth_password(my_ssh_session, "rohit", "password");
if (rc != SSH_AUTH_SUCCESS)
{
fprintf(stderr, "Error authenticating with password: %s\n",
ssh_get_error(my_ssh_session));
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
exit(-1);
}
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
}
编译:
gcc sshex.c -lssh
VALGRIND 运行:
root@rohit-Precision-T1500:/# valgrind --tool=memcheck --leak-check=full --show-leak-kinds=all --show-reachable=no ./a.out
==1937== Memcheck, a memory error detector
==1937== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==1937== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==1937== Command: ./a.out
==1937==
yahoo
==1937==
==1937== HEAP SUMMARY:
==1937== in use at exit: 14,256 bytes in 521 blocks
==1937== total heap usage: 1,339 allocs, 818 frees, 92,442 bytes allocated
==1937==
==1937== 24 bytes in 1 blocks are indirectly lost in loss record 507 of 520
==1937== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1937== by 0x56AAD32: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==1937== by 0x56E3A59: BN_new (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==1937== by 0x56E3F07: BN_bin2bn (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==1937== by 0x4E6079B: pki_signature_from_blob (pki_crypto.c:1384)
==1937== by 0x4E599F9: ssh_pki_import_signature_blob (pki.c:1262)
==1937== by 0x4E59A77: ssh_pki_signature_verify_blob (pki.c:1281)
==1937== by 0x4E578DE: ssh_packet_newkeys (packet_cb.c:186)
==1937== by 0x4E56E6F: ssh_packet_process (packet.c:422)
==1937== by 0x4E57374: ssh_packet_socket_callback (packet.c:326)
==1937== by 0x4E573B3: ssh_packet_socket_callback (packet.c:336)
==1937== by 0x4E5D951: ssh_socket_pollcallback (socket.c:290)
==1937==
==1937== 24 bytes in 1 blocks are indirectly lost in loss record 508 of 520
==1937== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1937== by 0x56AAD32: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==1937== by 0x56E3A59: BN_new (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==1937== by 0x56E3F07: BN_bin2bn (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==1937== by 0x4E607EC: pki_signature_from_blob (pki_crypto.c:1405)
==1937== by 0x4E599F9: ssh_pki_import_signature_blob (pki.c:1262)
==1937== by 0x4E59A77: ssh_pki_signature_verify_blob (pki.c:1281)
==1937== by 0x4E578DE: ssh_packet_newkeys (packet_cb.c:186)
==1937== by 0x4E56E6F: ssh_packet_process (packet.c:422)
==1937== by 0x4E57374: ssh_packet_socket_callback (packet.c:326)
==1937== by 0x4E573B3: ssh_packet_socket_callback (packet.c:336)
==1937== by 0x4E5D951: ssh_socket_pollcallback (socket.c:290)
==1937==
==1937== 32 bytes in 1 blocks are indirectly lost in loss record 511 of 520
==1937== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1937== by 0x56AAD32: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==1937== by 0x56E35E6: ??? (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==1937== by 0x56E3AD4: bn_expand2 (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==1937== by 0x56E3ECF: BN_bin2bn (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==1937== by 0x4E6079B: pki_signature_from_blob (pki_crypto.c:1384)
==1937== by 0x4E599F9: ssh_pki_import_signature_blob (pki.c:1262)
==1937== by 0x4E59A77: ssh_pki_signature_verify_blob (pki.c:1281)
==1937== by 0x4E578DE: ssh_packet_newkeys (packet_cb.c:186)
==1937== by 0x4E56E6F: ssh_packet_process (packet.c:422)
==1937== by 0x4E57374: ssh_packet_socket_callback (packet.c:326)
==1937== by 0x4E573B3: ssh_packet_socket_callback (packet.c:336)
==1937==
==1937== 32 bytes in 1 blocks are indirectly lost in loss record 512 of 520
==1937== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1937== by 0x56AAD32: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==1937== by 0x56E35E6: ??? (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==1937== by 0x56E3AD4: bn_expand2 (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==1937== by 0x56E3ECF: BN_bin2bn (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==1937== by 0x4E607EC: pki_signature_from_blob (pki_crypto.c:1405)
==1937== by 0x4E599F9: ssh_pki_import_signature_blob (pki.c:1262)
==1937== by 0x4E59A77: ssh_pki_signature_verify_blob (pki.c:1281)
==1937== by 0x4E578DE: ssh_packet_newkeys (packet_cb.c:186)
==1937== by 0x4E56E6F: ssh_packet_process (packet.c:422)
==1937== by 0x4E57374: ssh_packet_socket_callback (packet.c:326)
==1937== by 0x4E573B3: ssh_packet_socket_callback (packet.c:336)
==1937==
==1937== 48 bytes in 2 blocks are definitely lost in loss record 513 of 520
==1937== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1937== by 0x56AAD32: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==1937== by 0x56E3A59: BN_new (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==1937== by 0x574C358: ??? (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==1937== by 0x5750B1A: ??? (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==1937== by 0x5750BC5: ??? (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==1937== by 0x5750D79: ASN1_item_new (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==1937== by 0x4E60734: pki_signature_from_blob (pki_crypto.c:1347)
==1937== by 0x4E599F9: ssh_pki_import_signature_blob (pki.c:1262)
==1937== by 0x4E59A77: ssh_pki_signature_verify_blob (pki.c:1281)
==1937== by 0x4E578DE: ssh_packet_newkeys (packet_cb.c:186)
==1937== by 0x4E56E6F: ssh_packet_process (packet.c:422)
==1937==
==1937== 128 (16 direct, 112 indirect) bytes in 1 blocks are definitely lost in loss record 516 of 520
==1937== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1937== by 0x56AAD32: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==1937== by 0x5750D0B: ??? (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==1937== by 0x5750D79: ASN1_item_new (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==1937== by 0x4E60734: pki_signature_from_blob (pki_crypto.c:1347)
==1937== by 0x4E599F9: ssh_pki_import_signature_blob (pki.c:1262)
==1937== by 0x4E59A77: ssh_pki_signature_verify_blob (pki.c:1281)
==1937== by 0x4E578DE: ssh_packet_newkeys (packet_cb.c:186)
==1937== by 0x4E56E6F: ssh_packet_process (packet.c:422)
==1937== by 0x4E57374: ssh_packet_socket_callback (packet.c:326)
==1937== by 0x4E573B3: ssh_packet_socket_callback (packet.c:336)
==1937== by 0x4E5D951: ssh_socket_pollcallback (socket.c:290)
==1937==
==1937== LEAK SUMMARY:
==1937== definitely lost: 64 bytes in 3 blocks
==1937== indirectly lost: 112 bytes in 4 blocks
==1937== possibly lost: 0 bytes in 0 blocks
==1937== still reachable: 14,080 bytes in 514 blocks
==1937== suppressed: 0 bytes in 0 blocks
==1937== Reachable blocks (those to which a pointer was found) are not shown.
直接运行:
root@rohit-Precision-T1500:/# gcc sshex.c -lssh
root@rohit-Precision-T1500:/# ./a.out
already added
root@rohit-Precision-T1500:/#
Linux 版本:
root@rohit-Precision-T1500:/# uname -a
Linux rohit-Precision-T1500 3.13.0-43-generic #72-Ubuntu SMP Mon Dec 8 19:35:06 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
我已经尝试过这个。如果我删除函数 ssh_connect()
(当然还有其他后续函数),那么我就不会得到任何泄漏。因此,泄漏一定是由这个函数引发的。但是在我的小程序中,我使用了函数ssh_disconnect()
来释放ssh_connect()
分配的内存。那么,我怎样才能确定释放内存呢?
我想在我的软件中使用 SCP 功能,这将是一个长期存在的过程。因此,内存泄漏将成为一个严重的问题。根据我的理解,泄漏与 libcrypto
库有关,这可能不是 libssh
的泄漏。如果是或不是,消除这些泄漏的解决方案是什么?我哪里做错了?
最佳答案
正如我所料,事情发生了。这可能是 libcrypto
中的错误。我在 RHEL 6.5
中运行我的程序,valgrind 没有显示任何泄漏。
这是输出的片段:
[root@demo-hadoop test]# gcc sshex.c -lssh
[root@demo-hadoop test]# valgrind --tool=memcheck ./a.out
==23636== Memcheck, a memory error detector
==23636== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==23636== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==23636== Command: ./a.out
==23636==
already added
==23636==
==23636== HEAP SUMMARY:
==23636== in use at exit: 15,304 bytes in 532 blocks
==23636== total heap usage: 1,067 allocs, 535 frees, 82,199 bytes allocated
==23636==
==23636== LEAK SUMMARY:
==23636== definitely lost: 0 bytes in 0 blocks
==23636== indirectly lost: 0 bytes in 0 blocks
==23636== possibly lost: 0 bytes in 0 blocks
==23636== still reachable: 15,304 bytes in 532 blocks
==23636== suppressed: 0 bytes in 0 blocks
==23636== Rerun with --leak-check=full to see details of leaked memory
==23636==
==23636== For counts of detected and suppressed errors, rerun with: -v
==23636== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)
[root@demo-hadoop test]#
[root@demo-hadoop test]#
[root@demo-hadoop test]# uname -a
Linux demo-hadoop 2.6.32-431.el6.x86_64 #1 SMP Sun Nov 10 22:19:54 EST 2013 x86_64 x86_64 x86_64 GNU/Linux
所以,当然,这不是 libssh
中的错误。
关于c++ - libssh:简单实现中丢失的内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27839959/
我在 Ubuntu 10.04 LTS 上运行 Eclipse Galileo。今天 Ubuntu 在我身上崩溃了,重新启动后,我发现 Eclipse 已经完全失去了 Java Perspective
我使用配置了 sonata_user 的 SonataAdminBundle在 config.yml : sonata_user: impersonating: route:
我有 ubuntu 14.04 但它不见了 docker exec sudo docker exec -it ubuntu_bash bash 我希望在现有正在运行的 docker 容器中运行交互式
我正在使用 Ubuntu 8.04/32 位(作为虚拟机)。在一个不是 min 的项目上执行一些 make 时,我得到了错误: g++:/usr/lib/libstdc++.a: 没有这样的文件或目录
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许在 Stack Overflow 上提出有关通用计算硬件和软件的问题。您可以编辑问题,使其成为
我正在尝试获取有关我在 UIImagePicker 中选择的视频的一些数据。 因此,当它进入 UIImagePicker 委托(delegate)方法(如下)时,我知道我需要使用信息字典中的 UIIm
我的网站最近被可能的黑客行为删除了。我上传了备份的文件夹和数据库,但现在我的 View 没有显示。其他一切都有效。我想不出有什么变化,只是上传了几天前的备份。 这些字段在 admin/build/vi
我执行以下操作来设置我的 session ,这是有效的,因为 echo 出现了。但是当我转到下一页或另一页时, session 不存在吗?我做错了什么? $session_start(); if ($
我试图在 BigQuery 中使用这段代码,显然是从 GA 中获取数据,但 _TABLE_SUFFIX 似乎有问题。错误显示“错误:无法识别的名称:_TABLE_SUFFIX at [12:3]”您能
输入:8(2 5 6 9 10 2 7 4)预期输出:(2 7 9 9 12 5 7 6)实际输出:(2 7 9 9) 这是我的大学作业,也是我第一次在这里提问。我不知道为什么,但 10 没有扫描,有
$('div'); // 我在上面的代码中遇到错误。在检查 .js 代码时,我找不到名为 $ 的函数,但根据文档,应该有一个。 最佳答案 试试 http://ajax.googleapis.co
以下简单代码的输出对我来说有点奇怪。它错过了在控制台上打印的 0 到 100 之间的一些数字。 谁能解释一下为什么省略打印?我对并发编程完全陌生。 import java.util.concurren
我正在学习 xamarin 以构建移动应用程序,但我对 Xamarin 和移动应用程序一无所知。我打开了一个空白的移动应用程序,其中有“Hello world”示例并编辑了一些文本,但我的应用程序图标
我正在将一些值存储到 sqlite 数据库中。因此,数据是作为字符串从文本字段收集的,然后转换为 double 并持久化。 这是我试过的 NSDecimalNumber 答案; value.answe
我有一个奇怪的案例。突然,其中一个表中的一些记录(这么多记录)丢失了。首先,我认为这是由我的 PHP 脚本中的错误引起的。但是,我检查了一下,我的脚本中没有DELETE操作,只有UPDATE。有谁知道
我正在复制 NSString来自 NSDictionary进入本地NSString使用 [[NSString alloc] initWithString:] ,对其进行处理(删除一些字符),然后将其发
当保存在根文件夹中时,我的非常基本的 html 页面保持样式。为一个组创建了一个新文件夹,但是当我将页面移动到该文件夹时,它们似乎失去了与 css 文件的连接。 认为问题可能出在链接上,因为它现在
我需要使用 OpenCV 训练一些图像。但问题是,我找不到 opencv_createsamples 程序。我以正常方式安装了 OpenCV,因为我使用的是 Windows 7。这个 opencv_c
我有一个绑定(bind)到 JTable 的 ArrayList。之后 bindingGroup.unbind(); bindingGroup.bind(); (完成刷新数据)我丢失了表格单元格渲
所以基本上我们只是丢失了一个 keystore 和备份 keystore 。但是我们可能知道原始 keystore 的密码。 我的问题是,如果我们知道原始 keystore 的密码,我们可以重新生成
我是一名优秀的程序员,十分优秀!