我的代码中出现了内存泄漏问题
当我调用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);
我不知道哪里出错了。 请帮我。
答案 0 :(得分: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==
正如您所看到的,“泄漏”是加密lib(openssl的一部分)内部的,可能是错误警报。