gpt4 book ai didi

c++ - 关闭连接后,Valgrind 在 ssl 中显示内存泄漏

转载 作者:太空狗 更新时间:2023-10-29 12:14:56 26 4
gpt4 key购买 nike

我有一个客户端,它会不断地与服务器对话。当我通过 valgrind 运行它并得到以下报告时:

以下是我的 SSL 代码中仍然存在的漏洞。

==6850== 600 bytes in 1 blocks are still reachable in loss record 116 of 118
==6850== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6850== by 0x50F4D32: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==6850== by 0x517DA85: ERR_get_state (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==6850== by 0x517DB5D: ERR_put_error (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==6850== by 0x51AA9DC: PEM_read_bio (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==6850== by 0x51A8D39: PEM_X509_INFO_read_bio (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==6850== by 0x51B7A7D: X509_load_cert_crl_file (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==6850== by 0x51B7BAC: by_file_ctrl (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==6850== by 0x51AEF2E: X509_STORE_load_locations (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==6850== by 0x40AD4A: CloudConnection::establishConnection(int) (CloudConnection.cpp:1046)
==6850== by 0x409F1C: connectWithCloud() main.cpp:423)
==6850== by 0x409ADB: main (main.cpp:391)

==6850== 176 bytes in 1 blocks are still reachable in loss record 111 of 118
==6850== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6850== by 0x50F4D32: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==6850== by 0x517A8EF: lh_new (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==6850== by 0x50F5FF4: ex_data_check (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==6850== by 0x50F6084: def_get_class (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==6850== by 0x50F66C8: int_free_ex_data (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==6850== by 0x4E70D3C: SSL_free (in /lib/x86_64-linux-gnu/libssl.so.1.0.0)
==6850== by 0x40A6AE: CloudConnection::closeConnection() (CloudConnection.cpp:1554)
==6850== by 0x40D9BB: CloudConnection::receiveData() (CloudConnection.cpp:696)
==6850== by 0x40DB88: CloudConnection::receiveDataHelper(void*) (CloudConnection.cpp:276)
==6850== by 0x5477181: start_thread (pthread_create.c:312)
==6850== by 0x651FFBC: clone (clone.S:111)

这是我关闭 ssl 连接的代码片段。

    if (ssl) {
int count = 0;
while (err != 1) {
err = SSL_shutdown(ssl);
if (err != 1) {
LOGINFO("SSL_shutdown failed");
count++;
}
if (count == 3) {
LOGINFO("SSL_shutdown failed - final");
break;
}
sleep(1);
}
}
CRYPTO_cleanup_all_ex_data();

ERR_free_strings();
ERR_remove_state(0);
EVP_cleanup();

if (lockarray) {
kill_locks();
lockarray = NULL;
}
/* Terminate communication on a socket */
if (sock) {
err = close(sock);
if (-1 == err) {
LOG(Logger::ERROR, "Could not close the socket. "
"Returning error, but also making the structure NULL.");
//ssl = NULL;
//return -1;
}
}

/* Free the SSL structure */
if (ssl) {
SSL_free(ssl);
ssl = NULL;
}

/* Free the SSL_CTX structure */
if (ctx) {
SSL_CTX_free(ctx);
ctx = NULL;
}

请注意,logger 是我的自定义库

最佳答案

在谷歌搜索了很多之后这解决了我的问题

CRYPTO_cleanup_all_ex_data();
ERR_free_strings();
ERR_remove_state(0);
EVP_cleanup();

关于c++ - 关闭连接后,Valgrind 在 ssl 中显示内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29008145/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com