From 0647a241ac526314c7e2256dcfa8c34506ac148c Mon Sep 17 00:00:00 2001 From: Patric Stout Date: Sun, 23 Oct 2022 09:52:46 +0000 Subject: [PATCH] fix(connection): thread could access members before constructor was done initializing --- src/Connection.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Connection.cpp b/src/Connection.cpp index 59d18b8..60e862b 100644 --- a/src/Connection.cpp +++ b/src/Connection.cpp @@ -18,10 +18,14 @@ TrueMQTT::Client::Impl::Connection::Connection(Client::Impl &impl) : m_impl(impl), - m_thread_read(&Connection::runRead, this), - m_thread_write(&Connection::runWrite, this), m_backoff(impl.m_connection_backoff) { + // This has to be delayed to inside the ctor body, as otherwise other + // parts of the object might not been initialized yet, and the threads + // might already start running (and using these not initialized parts). + m_thread_read = std::thread(&Connection::runRead, this); + m_thread_write = std::thread(&Connection::runWrite, this); + pthread_setname_np(m_thread_read.native_handle(), "TrueMQTT::Read"); pthread_setname_np(m_thread_write.native_handle(), "TrueMQTT::Write"); } -- libgit2 0.21.4