From 14b92bea172df18eb3f577e99a877fd6288379eb Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Wed, 8 Feb 2023 19:05:18 +0100 Subject: Fix crash on firing timer when object is gone already --- whiteboard.cpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'whiteboard.cpp') diff --git a/whiteboard.cpp b/whiteboard.cpp index 2c8a47b..88f4f3b 100644 --- a/whiteboard.cpp +++ b/whiteboard.cpp @@ -104,6 +104,13 @@ public: { } + ~session() + { + if (m_stats_timer) + m_stats_timer->cancel(); + m_stats_timer = nullptr; + } + void do_read_handshake() { // Set a decorator to change the Server of the handshake @@ -273,13 +280,15 @@ public: void stats_callback(const boost::system::error_code&) { - auto data{std::make_shared(stats_xml())}; - auto buffer{std::make_shared(data->data(), data->size())}; - m_ws->async_write(*buffer, boost::asio::bind_executor(m_ws->next_layer().get_executor(), - boost::beast::bind_front_handler(&session::on_write_stats, shared_from_this(), data, buffer))); + if (m_stats_timer) { + auto data{std::make_shared(stats_xml())}; + auto buffer{std::make_shared(data->data(), data->size())}; + m_ws->async_write(*buffer, boost::asio::bind_executor(m_ws->next_layer().get_executor(), + boost::beast::bind_front_handler(&session::on_write_stats, shared_from_this(), data, buffer))); - m_stats_timer->expires_at(m_stats_timer->expires_at() + boost::asio::chrono::seconds(5)); - m_stats_timer->async_wait(boost::beast::bind_front_handler(&session::stats_callback, this)); + m_stats_timer->expires_at(m_stats_timer->expires_at() + boost::asio::chrono::seconds(5)); + m_stats_timer->async_wait(boost::beast::bind_front_handler(&session::stats_callback, this)); + } } void setup_stats_timer() -- cgit v1.2.3