@@ -129,6 +129,8 @@ TEST_F(TestMemoryStrategy, get_subscription_by_handle) {
129129 EXPECT_EQ (
130130 nullptr ,
131131 memory_strategy ()->get_subscription_by_handle (subscription_handle, weak_groups_to_nodes));
132+ // Clear all handles to cleanup when node is destroyed
133+ memory_strategy ()->clear_handles ();
132134 } // Node goes out of scope
133135 EXPECT_EQ (
134136 nullptr ,
@@ -181,6 +183,8 @@ TEST_F(TestMemoryStrategy, get_service_by_handle) {
181183 EXPECT_EQ (
182184 nullptr ,
183185 memory_strategy ()->get_service_by_handle (service_handle, weak_groups_to_nodes));
186+ // Clear all handles to cleanup when node is destroyed
187+ memory_strategy ()->clear_handles ();
184188 } // Node goes out of scope
185189 EXPECT_EQ (
186190 nullptr ,
@@ -228,6 +232,11 @@ TEST_F(TestMemoryStrategy, get_client_by_handle) {
228232 EXPECT_EQ (
229233 nullptr ,
230234 memory_strategy ()->get_client_by_handle (client_handle, weak_groups_to_nodes));
235+ // Clear all handles to cleanup when node is destroyed
236+ memory_strategy ()->clear_handles ();
237+ // reset client handle before node destruction, otherwise it tries to access node
238+ // via weak_ptr in its deleter
239+ client_handle.reset ();
231240 } // Node goes out of scope
232241 EXPECT_EQ (
233242 nullptr ,
@@ -276,6 +285,8 @@ TEST_F(TestMemoryStrategy, get_timer_by_handle) {
276285 EXPECT_EQ (
277286 nullptr ,
278287 memory_strategy ()->get_timer_by_handle (timer_handle, weak_groups_to_nodes));
288+ // Clear all handles to cleanup when node is destroyed
289+ memory_strategy ()->clear_handles ();
279290 } // Node goes out of scope
280291 EXPECT_EQ (
281292 nullptr ,
@@ -375,6 +386,8 @@ TEST_F(TestMemoryStrategy, get_group_by_subscription) {
375386 EXPECT_EQ (
376387 callback_group,
377388 memory_strategy ()->get_group_by_subscription (subscription, weak_groups_to_nodes));
389+ // Clear all handles to cleanup when node is destroyed
390+ memory_strategy ()->clear_handles ();
378391 } // Node goes out of scope
379392 // NodeBase(SubscriptionBase->rcl_node_t->NodeBase) is still alive.
380393 EXPECT_EQ (
@@ -420,6 +433,8 @@ TEST_F(TestMemoryStrategy, get_group_by_service) {
420433 EXPECT_EQ (
421434 nullptr ,
422435 memory_strategy ()->get_group_by_service (service, weak_groups_to_nodes));
436+ // Clear all handles to cleanup when node is destroyed
437+ memory_strategy ()->clear_handles ();
423438 } // Node goes out of scope
424439 EXPECT_EQ (
425440 nullptr ,
@@ -459,6 +474,8 @@ TEST_F(TestMemoryStrategy, get_group_by_client) {
459474 EXPECT_EQ (
460475 nullptr ,
461476 memory_strategy ()->get_group_by_client (client, weak_groups_to_nodes));
477+ // Clear all handles to cleanup when node is destroyed
478+ memory_strategy ()->clear_handles ();
462479 } // Node goes out of scope
463480 EXPECT_EQ (
464481 nullptr ,
@@ -498,6 +515,8 @@ TEST_F(TestMemoryStrategy, get_group_by_timer) {
498515 EXPECT_EQ (
499516 nullptr ,
500517 memory_strategy ()->get_group_by_timer (timer, weak_groups_to_nodes));
518+ // Clear all handles to cleanup when node is destroyed
519+ memory_strategy ()->clear_handles ();
501520 } // Node goes out of scope
502521 EXPECT_EQ (
503522 nullptr ,
@@ -536,6 +555,8 @@ TEST_F(TestMemoryStrategy, get_group_by_waitable) {
536555 EXPECT_EQ (
537556 nullptr ,
538557 memory_strategy ()->get_group_by_waitable (waitable, weak_groups_to_nodes));
558+ // Clear all handles to cleanup when node is destroyed
559+ memory_strategy ()->clear_handles ();
539560 } // Node goes out of scope
540561 EXPECT_EQ (
541562 nullptr ,
0 commit comments