最近我开始研究C++中的内存泄漏问题,所以我可能会问一些天真的问题.
我有一个使用OpenSSL的c ++库 - 我的任务是检查此lib中是否存在内存泄漏.我运行Visual Leak Detector来检查内存泄漏.
我看到调用SSL_library_init();并且 SSL_load_error_strings();正在引导泄漏 - 快速谷歌搜索显示在使用结束时我必须调用以下内容:
CONF_modules_free();
ERR_remove_state(0);
ENGINE_cleanup();
CONF_modules_unload(1);
ERR_free_strings();
EVP_cleanup();
CRYPTO_cleanup_all_ex_data();
Run Code Online (Sandbox Code Playgroud)
泄漏确实减少了,但仍有两次泄漏(VLD工具显示我)因为SSL_library_init呼叫而发生.
有谁知道我还要做什么才能解除所有内存泄漏?
我 现在按照本指南手动构建openssl(静态库)当我尝试将我的MFC测试应用程序与libeay32.lib链接时出现以下错误:
1>Linking...
1>libeay32.lib(e_capi.obj) : error LNK2019: unresolved external symbol __imp__CertFreeCertificateContext@4 referenced in function _capi_free_key
1>libeay32.lib(e_capi.obj) : error LNK2019: unresolved external symbol __imp__CertGetCertificateContextProperty@16 referenced in function _capi_get_prov_info
1>libeay32.lib(e_capi.obj) : error LNK2019: unresolved external symbol __imp__CertOpenStore@20 referenced in function _capi_open_store
1>libeay32.lib(e_capi.obj) : error LNK2019: unresolved external symbol __imp__CertFindCertificateInStore@24 referenced in function _capi_find_cert
1>libeay32.lib(e_capi.obj) : error LNK2019: unresolved external symbol __imp__CertEnumCertificatesInStore@8 referenced in function _capi_find_cert
1>libeay32.lib(e_capi.obj) : error LNK2019: unresolved external symbol __imp__CertCloseStore@8 referenced in function _capi_find_key
1>libeay32.lib(e_capi.obj) : error …Run Code Online (Sandbox Code Playgroud) 这个问题涉及 OpenSSL 1.1.0+。在我使用的代码示例中std::string_view,这意味着C++17. 这不是必需的,任何事情C++11都可以,我只是懒得将 const char* buf和std::size_t len作为单独的变量。
#include <string_view>
#include <openssl/err.h>
#include <openssl/ssl.h>
void startup()
{
SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_ssl_algorithms();
ERR_load_crypto_strings();
}
void shutdown()
{
ERR_free_strings();
EVP_cleanup();
}
void thread_shutdown()
{
CRYPTO_cleanup_all_ex_data();
}
void run_per_thread()
{
// intial non SSL stuff
int sockfd = get_connected_socket();
std::string_view hostname = get_hostname();
std::string_view buffer = get_buffer();
// SSL context setup
auto ssl_ctx = SSL_CTX_new(TLS_client_method());
auto ssl_ctx_options = SSL_OP_SINGLE_DH_USE || SSL_OP_NO_SSLv3;
SSL_CTX_set_options(ssl_ctx, ssl_ctx_options);
SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_NONE, …Run Code Online (Sandbox Code Playgroud)