我试图在win 7 32位上使用boost :: asio库连接websocket。通常,我得到两个与openssl相关的内存泄漏但是如果我将boost :: asio :: io_service :: run方法放在一个单独的线程中,我会得到6个内存泄漏,其中两个泄漏是openssl lib。
这是我的代码示例。
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/array.hpp>
#include <boost/thread.hpp>
#include <iostream>
#include <string>
#include <vld.h>
boost::asio::io_service io_service;
boost::asio::ip::tcp::resolver resolver(io_service);
boost::array<char, 4096> buffer;
boost::thread thr; // thread object
boost::asio::ssl::context ctx(io_service, boost::asio::ssl::context::tlsv1_client);
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssl_sock(io_service, ctx);
void read_handler(const boost::system::error_code &ec, std::size_t bytes_transferred)
{
boost::system::error_code ec1;
ssl_sock.lowest_layer().close(ec1);
}
void connect_handler(const boost::system::error_code &ec)
{
boost::system::error_code ec1;
ssl_sock.handshake(boost::asio::ssl::stream<boost::asio::ip::tcp::socket>::client, ec1);
std::string m = "GET / HTTP/1.1\r\n"
"Sec-WebSocket-Key: GCeEn+pBGdrdj4mcrtBukA==\r\n"
"Upgrade: websocket\r\n"
"Sec-WebSocket-Extensions: x-webkit-deflate-frame"
"Connection: Upgrade\r\n"
"Sec-WebSocket-Version: 13\r\n"
"\r\n";
if(!ec)
{
boost::asio::write(ssl_sock, boost::asio::buffer(m));
ssl_sock.async_read_some(boost::asio::buffer(buffer), read_handler);
}
std::cout<<ec.message().c_str();
}
void resolve_handler(const boost::system::error_code &ec, boost::asio::ip::tcp::resolver::iterator it)
{
if(!ec)
{
ssl_sock.lowest_layer().async_connect(*it, connect_handler);
}
std::cout<<ec.message().c_str();
}
int main()
{
ctx.set_verify_mode(boost::asio::ssl::verify_none);
boost::asio::ip::tcp::resolver::query query("echo.websocket.org", "443");
resolver.async_resolve(query, resolve_handler);
thr = boost::thread( boost::bind(&boost::asio::io_service::run, &io_service) );
thr.join();
// io_service.run();
}
在读处理程序上,我只是关闭了连接。总的来说,我只是创建和关闭连接。当错误代码为0时,套接字正在正常关闭。 以下是内存泄漏。
d:\cfiles\projects\winssl\openssl-1.0.1e\crypto\mem.c (79): WebsocketPPTest.exe!default_malloc_ex + 0xB bytes
g:\websocketpptest\boost\asio\ssl\detail\impl\openssl_init.ipp (127): WebsocketPPTest.exe!boost::asio::ssl::detail::openssl_init_base::instance + 0x4A bytes
g:\websocketpptest\boost\asio\ssl\detail\openssl_init.hpp (61): WebsocketPPTest.exe!boost::asio::ssl::detail::openssl_init<1>::openssl_init<1> + 0x5A bytes
g:\websocketpptest\boost\asio\ssl\impl\context.ipp (171): WebsocketPPTest.exe!boost::asio::ssl::context::context + 0x6D bytes
g:\websocketpptest\src\boostsocket.cpp (19): WebsocketPPTest.exe!`dynamic initializer for 'ctx'' + 0x2F bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crt0dat.c (873): WebsocketPPTest.exe!_initterm
f:\dd\vctools\crt_bld\self_x86\crt\src\crt0dat.c (288): WebsocketPPTest.exe!_cinit + 0xF bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crt0.c (262): WebsocketPPTest.exe!__tmainCRTStartup + 0x7 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crt0.c (189): WebsocketPPTest.exe!mainCRTStartup
0x76AF336A (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
0x77309F72 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
0x77309F45 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
Data:
00 00 00 00 40 68 71 00 01 00 00 00 04 00 00 00 ....@hq. ........
E0 96 4C 00 ..L..... ........
---------- Block 221 at 0x00716840: 16 bytes ----------
Call Stack:
d:\cfiles\projects\winssl\openssl-1.0.1e\crypto\mem.c (79): WebsocketPPTest.exe!default_malloc_ex + 0xB bytes
g:\websocketpptest\boost\asio\ssl\detail\impl\openssl_init.ipp (127): WebsocketPPTest.exe!boost::asio::ssl::detail::openssl_init_base::instance + 0x4A bytes
g:\websocketpptest\boost\asio\ssl\detail\openssl_init.hpp (61): WebsocketPPTest.exe!boost::asio::ssl::detail::openssl_init<1>::openssl_init<1> + 0x5A bytes
g:\websocketpptest\boost\asio\ssl\impl\context.ipp (171): WebsocketPPTest.exe!boost::asio::ssl::context::context + 0x6D bytes
g:\websocketpptest\src\boostsocket.cpp (19): WebsocketPPTest.exe!`dynamic initializer for 'ctx'' + 0x2F bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crt0dat.c (873): WebsocketPPTest.exe!_initterm
f:\dd\vctools\crt_bld\self_x86\crt\src\crt0dat.c (288): WebsocketPPTest.exe!_cinit + 0xF bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crt0.c (262): WebsocketPPTest.exe!__tmainCRTStartup + 0x7 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crt0.c (189): WebsocketPPTest.exe!mainCRTStartup
0x76AF336A (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
0x77309F72 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
0x77309F45 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
Data:
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
---------- Block 5344 at 0x02A23008: 96 bytes ----------
Call Stack:
Data:
E0 31 A2 02 40 16 3F 00 00 16 3F 00 08 00 00 00 .1..@.?. ..?.....
10 00 00 00 00 00 00 00 08 00 00 00 00 02 00 00 ........ ........
00 01 00 00 01 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 1E 00 00 00 1C 00 00 00 ........ ........
01 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 ........ ........
1C 00 00 00 00 00 00 00 1D 00 00 00 00 00 00 00 ........ ........
---------- Block 5346 at 0x02A230A8: 12 bytes ----------
Call Stack:
Data:
D0 6A A2 02 00 00 00 00 E0 BE 01 00 .j...... ........
---------- Block 5345 at 0x02A231E0: 64 bytes ----------
Call Stack:
Data:
A8 30 A2 02 00 00 00 00 00 00 00 00 00 00 00 00 .0...... ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
---------- Block 5343 at 0x02A26AD0: 400 bytes ----------
Call Stack:
Data:
00 00 00 00 60 22 00 00 00 00 00 00 00 00 00 00 ....`".. ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........