File tree Expand file tree Collapse file tree 3 files changed +32
-0
lines changed
experimental/executors/events_executor Expand file tree Collapse file tree 3 files changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -178,6 +178,12 @@ struct ExecutorEntitiesCollection
178178
179179 // / Clear the entities collection
180180 void clear ();
181+
182+ // / Remove entities that have expired weak ownership
183+ /* *
184+ * \return The total number of removed entities
185+ */
186+ size_t remove_expired_entities ();
181187};
182188
183189// / Build an entities collection from callback groups
Original file line number Diff line number Diff line change @@ -39,6 +39,30 @@ void ExecutorEntitiesCollection::clear()
3939 waitables.clear ();
4040}
4141
42+ size_t ExecutorEntitiesCollection::remove_expired_entities ()
43+ {
44+ auto remove_entities = [](auto & collection) -> size_t {
45+ size_t removed = 0 ;
46+ for (auto it = collection.begin (); it != collection.end (); ) {
47+ if (it->second .entity .expired ()) {
48+ ++removed;
49+ it = collection.erase (it);
50+ } else {
51+ ++it;
52+ }
53+ }
54+ return removed;
55+ };
56+
57+ return
58+ remove_entities (subscriptions) +
59+ remove_entities (timers) +
60+ remove_entities (guard_conditions) +
61+ remove_entities (clients) +
62+ remove_entities (services) +
63+ remove_entities (waitables);
64+ }
65+
4266void
4367build_entities_collection (
4468 const std::vector<rclcpp::CallbackGroup::WeakPtr> & callback_groups,
Original file line number Diff line number Diff line change @@ -431,6 +431,8 @@ EventsExecutor::refresh_current_collection(
431431 // Acquire lock before modifying the current collection
432432 std::lock_guard<std::recursive_mutex> lock (collection_mutex_);
433433
434+ current_entities_collection_->remove_expired_entities ();
435+
434436 current_entities_collection_->timers .update (
435437 new_collection.timers ,
436438 [this ](rclcpp::TimerBase::SharedPtr timer) {timers_manager_->add_timer (timer);},
You can’t perform that action at this time.
0 commit comments