Skip to content

Commit 06d83e8

Browse files
committed
Fixing memory leak from OpenSSL when using websocket_client that occurrs per connection.
1 parent e9f6a1f commit 06d83e8

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

Release/src/websockets/client/ws_client_wspp.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ class wspp_callback_client : public websocket_client_callback_impl, public std::
168168
sslContext->set_default_verify_paths();
169169
sslContext->set_options(boost::asio::ssl::context::default_workarounds);
170170
sslContext->set_verify_mode(boost::asio::ssl::context::verify_peer);
171+
171172
#if defined(__APPLE__) || (defined(ANDROID) || defined(__ANDROID__)) || defined(_WIN32)
172173
m_openssl_failed = false;
173174
#endif
@@ -191,6 +192,14 @@ class wspp_callback_client : public websocket_client_callback_impl, public std::
191192
return rfc2818(preverified, verifyCtx);
192193
});
193194

195+
// OpenSSL stores some per thread state that never will be cleaned up until
196+
// the dll is unloaded. If static linking, like we do, the state isn't cleaned up
197+
// at all and will be reported as leaks.
198+
// See http://www.openssl.org/support/faq.html#PROG13
199+
// This is necessary here because it is called on the user's thread calling connect(...)
200+
// eventually through websocketpp::client::get_connection(...)
201+
ERR_remove_thread_state(nullptr);
202+
194203
return sslContext;
195204
});
196205
return connect_impl<websocketpp::config::asio_tls_client>();
@@ -306,6 +315,12 @@ class wspp_callback_client : public websocket_client_callback_impl, public std::
306315
#if defined(__ANDROID__)
307316
crossplat::JVM.load()->DetachCurrentThread();
308317
#endif
318+
319+
// OpenSSL stores some per thread state that never will be cleaned up until
320+
// the dll is unloaded. If static linking, like we do, the state isn't cleaned up
321+
// at all and will be reported as leaks.
322+
// See http://www.openssl.org/support/faq.html#PROG13
323+
ERR_remove_thread_state(nullptr);
309324
});
310325
return pplx::create_task(m_connect_tce);
311326
}

Release/tests/common/TestRunner/test_runner.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,11 +510,11 @@ int main(int argc, char* argv[])
510510
{
511511
#ifdef _WIN32
512512
// Add standard error as output as well.
513-
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_WNDW);
513+
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_WNDW | _CRTDBG_MODE_DEBUG);
514514
_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
515-
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_WNDW);
515+
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_WNDW | _CRTDBG_MODE_DEBUG);
516516
_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
517-
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
517+
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
518518
_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
519519

520520
// The test runner built with WinRT support might be used on a pre Win8 machine.

0 commit comments

Comments
 (0)