From 493cf5648faff9a27c1604ecf422986fb7d93fcc Mon Sep 17 00:00:00 2001 From: Mingbo <1418561105@qq.com> Date: Tue, 25 Jun 2024 13:42:54 +0800 Subject: [PATCH] Repair crash when event comes between default listener set and enable() called. (#493) --- .../cyclonedds/core/EntityDelegate.hpp | 1 + .../cyclonedds/core/EntityDelegate.cpp | 25 ++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/ddscxx/include/org/eclipse/cyclonedds/core/EntityDelegate.hpp b/src/ddscxx/include/org/eclipse/cyclonedds/core/EntityDelegate.hpp index 000f917a..dfff0684 100644 --- a/src/ddscxx/include/org/eclipse/cyclonedds/core/EntityDelegate.hpp +++ b/src/ddscxx/include/org/eclipse/cyclonedds/core/EntityDelegate.hpp @@ -131,6 +131,7 @@ class OMG_DDS_API EntityDelegate : dds::core::status::StatusMask listener_mask; long callback_count; dds_listener_t *listener_callbacks; + dds_listener_t *temp_listener_callbacks; private: void *listener; diff --git a/src/ddscxx/src/org/eclipse/cyclonedds/core/EntityDelegate.cpp b/src/ddscxx/src/org/eclipse/cyclonedds/core/EntityDelegate.cpp index 284d4f5f..cabb6db8 100644 --- a/src/ddscxx/src/org/eclipse/cyclonedds/core/EntityDelegate.cpp +++ b/src/ddscxx/src/org/eclipse/cyclonedds/core/EntityDelegate.cpp @@ -36,6 +36,7 @@ org::eclipse::cyclonedds::core::EntityDelegate::EntityDelegate() : enabled_(false), listener_mask(0), listener_callbacks(NULL), + temp_listener_callbacks(NULL), listener(NULL) { this->callback_mutex = dds_alloc (sizeof (ddsrt_mutex_t)); @@ -58,6 +59,15 @@ org::eclipse::cyclonedds::core::EntityDelegate::~EntityDelegate() } this->listener_callbacks = NULL; + if (this->temp_listener_callbacks != NULL) + { + void *arg; + dds_lget_data_available_arg(this->temp_listener_callbacks, nullptr, &arg, nullptr); + dds_delete_listener(this->temp_listener_callbacks); + delete reinterpret_cast(arg); + } + this->temp_listener_callbacks = NULL; + ddsrt_cond_destroy (static_cast(this->callback_cond)); ddsrt_mutex_destroy (static_cast(this->callback_mutex)); dds_free (this->callback_cond); @@ -228,16 +238,23 @@ org::eclipse::cyclonedds::core::EntityDelegate::listener_set( } // Delete previous ddsc listener callbacks object - if (this->listener_callbacks != NULL) - { + if (this->enabled_ && this->listener_callbacks != nullptr) { void *prev_arg; dds_lget_data_available_arg(this->listener_callbacks, nullptr, &prev_arg, nullptr); dds_delete_listener(this->listener_callbacks); delete reinterpret_cast(prev_arg); + } else if (this->temp_listener_callbacks != nullptr) { + void *prev_arg; + dds_lget_data_available_arg(this->temp_listener_callbacks, nullptr, &prev_arg, nullptr); + dds_delete_listener(this->temp_listener_callbacks); + delete reinterpret_cast(prev_arg); } - // Store new listener - this->listener_callbacks = callbacks; + if (this->listener_callbacks == nullptr || this->enabled_) { + this->listener_callbacks = callbacks; + } else { + this->temp_listener_callbacks = callbacks; + } } void * org::eclipse::cyclonedds::core::EntityDelegate::listener_get () const