diff --git a/src/cpp/rtps/writer/StatefulWriter.cpp b/src/cpp/rtps/writer/StatefulWriter.cpp index 693123459ea..d8fbca2ed7e 100644 --- a/src/cpp/rtps/writer/StatefulWriter.cpp +++ b/src/cpp/rtps/writer/StatefulWriter.cpp @@ -312,9 +312,7 @@ StatefulWriter::~StatefulWriter() { logInfo(RTPS_WRITER, "StatefulWriter destructor"); - // This must be the first action, because free CacheChange_t from async thread. - deinit(); - + // Disable timed events, because their callbacks use cache changes if (disable_positive_acks_) { delete(ack_event_); @@ -327,6 +325,15 @@ StatefulWriter::~StatefulWriter() nack_response_event_ = nullptr; } + if (periodic_hb_event_ != nullptr) + { + delete(periodic_hb_event_); + periodic_hb_event_ = nullptr; + } + + // This must be the next action, as it frees CacheChange_t from the async thread. + deinit(); + // Stop all active proxies and pass them to the pool { std::lock_guard guard(mp_mutex); @@ -353,13 +360,6 @@ StatefulWriter::~StatefulWriter() } } - // Destroy heartbeat event - if (periodic_hb_event_ != nullptr) - { - delete(periodic_hb_event_); - periodic_hb_event_ = nullptr; - } - // Delete all proxies in the pool for (ReaderProxy* remote_reader : matched_readers_pool_) {