可能是 boost ssl 握手功能有一些内存泄漏
Posted
技术标签:
【中文标题】可能是 boost ssl 握手功能有一些内存泄漏【英文标题】:may be boost ssl handshake function have some memory leak 【发布时间】:2013-08-09 01:46:52 【问题描述】:我的代码中有内存泄漏问题。 当我调用 socket.handshake() 函数时, 然后退出主线程,我得到了内存泄漏。 但是,删除“socket.handshake”,然后 运行&退出主线程,内存泄漏问题消失。
1.示例代码:内存泄漏
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/thread.hpp>
using namespace boost;
void ThSslEchoClient()
try
asio::io_service ios;
asio::ssl::context context(boost::asio::ssl::context::sslv23);
context.set_options(
asio::ssl::context::default_workarounds
| asio::ssl::context::no_sslv2
| asio::ssl::context::single_dh_use);
asio::ssl::stream<boost::asio::ip::tcp::socket> socket(ios, context);
boost::system::error_code ec;
boost::asio::ip::tcp::endpoint host(boost::asio::ip::address::from_string("127.0.0.1"), 13);
socket.lowest_layer().connect(host, ec);
// REMOVE THE FOLLOWING LINE FOR COMPARISON
socket.handshake(asio::ssl::stream_base::client, ec);
if (ec)
throw boost::system::system_error(ec);
catch (std::exception& _e)
std::cerr << _e.what() << std::endl;
void TestBoostAsioSslEchoClient()
boost::thread_group tg;
tg.create_thread(ThSslEchoClient);
tg.join_all();
2。示例代码:没有内存泄漏。 (只需删除“socket.handshake”)
// REMOVE THE FOLLOWING LINE FOR COMPARISON
//socket.handshake(asio::ssl::stream_base::client, ec);
我不知道哪里错了。 请帮我。
【问题讨论】:
您是如何确定存在内存泄漏的? 【参考方案1】:我在我的电脑上看不出有什么不同。
我在端口 13 上伪造了一个小服务器:
while true; do date | sudo netcat -l -p 13; done
我运行程序:
make && valgrind ./test
没有握手的输出:
sehe@desktop:/tmp$ valgrind ./test
==15878== Memcheck, a memory error detector
==15878== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==15878== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==15878== Command: ./test
==15878==
==15878==
==15878== HEAP SUMMARY:
==15878== in use at exit: 64 bytes in 2 blocks
==15878== total heap usage: 3,348 allocs, 3,346 frees, 207,156 bytes allocated
==15878==
==15878== LEAK SUMMARY:
==15878== definitely lost: 0 bytes in 0 blocks
==15878== indirectly lost: 0 bytes in 0 blocks
==15878== possibly lost: 0 bytes in 0 blocks
==15878== still reachable: 64 bytes in 2 blocks
==15878== suppressed: 0 bytes in 0 blocks
==15878== Rerun with --leak-check=full to see details of leaked memory
==15878==
==15878== For counts of detected and suppressed errors, rerun with: -v
==15878== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
通过握手,它会打印:
unkn protocol
泄漏报告握手:
140 ==16017== 24 bytes in 1 blocks are still reachable in loss record 1 of 6
141 ==16017== at 0x4C2B3F8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
142 ==16017== by 0x60031FF: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
154 ==16017== 32 bytes in 1 blocks are still reachable in loss record 2 of 6
155 ==16017== at 0x4C2B3F8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
156 ==16017== by 0x60031FF: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
168 ==16017== 32 bytes in 1 blocks are still reachable in loss record 3 of 6
169 ==16017== at 0x4C2B3F8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
170 ==16017== by 0x60031FF: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
182 ==16017== 128 bytes in 1 blocks are still reachable in loss record 4 of 6
183 ==16017== at 0x4C2B3F8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
184 ==16017== by 0x60031FF: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
196 ==16017== 176 bytes in 1 blocks are still reachable in loss record 5 of 6
197 ==16017== at 0x4C2B3F8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
198 ==16017== by 0x60031FF: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
210 ==16017== 600 bytes in 1 blocks are still reachable in loss record 6 of 6
211 ==16017== at 0x4C2B3F8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
212 ==16017== by 0x60031FF: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
228 ==16017== still reachable: 992 bytes in 6 blocks
229 ==16017== suppressed: 0 bytes in 0 blocks
230 ==16017==
如您所见,“泄漏”位于加密库(openssl 的一部分)内部,可能是错误警报。
【讨论】:
谢谢,但我的工作环境是 windows。并在 Visual Studio 2012 下运行示例代码,然后提醒一些内存泄漏。好吧,忽略它? 请像这样测试 =============================== boost::thread_group tg; tg.create_thread(ThSslEchoClient); tg.join_all(); ================================ @user2666399 是什么让你认为我改变了测试?!另外,我建议您找到一个类似的工具来报告您的平台泄漏了哪些内存。我的预期是 99% 它将具有相同的源(可能是一些加密 RNG 状态,它获得线程本地初始化并且没有被释放) 我认为 libcrypto 内置了内存泄漏报告。尝试CRYPTO_memcheck_ctrl(CRYPTO_MEM_CHECK_ON);
,然后(退出时)CRYPTO_mem_leaks(stderr);
@user2666399 也许我正在使用thread_group。 是什么让你认为我改变了测试?!。当然平台不一样。我正在努力提供帮助。以上是关于可能是 boost ssl 握手功能有一些内存泄漏的主要内容,如果未能解决你的问题,请参考以下文章
返回 ndarray 的字典会导致使用 boost python 的内存泄漏
使用 Eigen 和 boost::thread 在 C++ 中出现奇怪的内存泄漏
在TLS / SSL握手(证书等)中泄露了哪些信息? [关闭]