gpt4 book ai didi

c++ - 可能是 boost ssl 握手函数有一些内存泄漏

转载 作者:行者123 更新时间:2023-11-28 07:28:51 24 4
gpt4 key购买 nike

我的代码中有内存泄漏问题。
当我调用 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);

不知道哪里错了。请帮我。

最佳答案

我在我的 PC 上看不出有什么不同。

我在端口 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 的一部分)内部的,很可能是错误警报。

关于c++ - 可能是 boost ssl 握手函数有一些内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18138987/

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