Skip to content

Commit

Permalink
Repair crash when event comes between default listener set and enable…
Browse files Browse the repository at this point in the history
…() called. (#493)
  • Loading branch information
ruoruoniao committed Jun 25, 2024
1 parent eae90af commit 493cf56
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 21 additions & 4 deletions src/ddscxx/src/org/eclipse/cyclonedds/core/EntityDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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<org::eclipse::cyclonedds::core::ListenerArg *>(arg);
}
this->temp_listener_callbacks = NULL;

ddsrt_cond_destroy (static_cast<ddsrt_cond_t*>(this->callback_cond));
ddsrt_mutex_destroy (static_cast<ddsrt_mutex_t*>(this->callback_mutex));
dds_free (this->callback_cond);
Expand Down Expand Up @@ -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<org::eclipse::cyclonedds::core::ListenerArg *>(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<org::eclipse::cyclonedds::core::ListenerArg *>(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
Expand Down

0 comments on commit 493cf56

Please sign in to comment.