diff --git a/rclcpp/include/rclcpp/experimental/buffers/ring_buffer_implementation.hpp b/rclcpp/include/rclcpp/experimental/buffers/ring_buffer_implementation.hpp index 69ed8fcb1c..4867b080f0 100644 --- a/rclcpp/include/rclcpp/experimental/buffers/ring_buffer_implementation.hpp +++ b/rclcpp/include/rclcpp/experimental/buffers/ring_buffer_implementation.hpp @@ -231,7 +231,6 @@ class RingBufferImplementation : public BufferImplementationBase inline void clear_() { - ring_buffer_.clear(); size_ = 0; read_index_ = 0; write_index_ = capacity_ - 1; diff --git a/rclcpp/test/rclcpp/test_ring_buffer_implementation.cpp b/rclcpp/test/rclcpp/test_ring_buffer_implementation.cpp index 8a5e0be55e..687c34428b 100644 --- a/rclcpp/test/rclcpp/test_ring_buffer_implementation.cpp +++ b/rclcpp/test/rclcpp/test_ring_buffer_implementation.cpp @@ -165,6 +165,42 @@ TEST(TestRingBufferImplementation, test_buffer_clear) { EXPECT_EQ('d', d); } +namespace detail +{ + +struct Tracked +{ + inline static int destroyed = 0; + int value; + + Tracked() + : value(0) {} + explicit Tracked(int v) + : value(v) {} + ~Tracked() {destroyed++;} +}; +} // namespace detail + +TEST(TestRingBufferImplementation, clear_is_non_destructive) +{ + detail::Tracked::destroyed = 0; + + rclcpp::experimental::buffers::RingBufferImplementation rb(2); + + rb.enqueue(detail::Tracked(1)); + rb.enqueue(detail::Tracked(2)); + + detail::Tracked::destroyed = 0; + + rb.clear(); + + EXPECT_EQ(detail::Tracked::destroyed, 0); + + // Outside buffer should be removed + rb.enqueue(detail::Tracked(3)); + EXPECT_GE(detail::Tracked::destroyed, 1); +} + TEST(TestRingBufferImplementation, handle_nullptr_deletion) { rclcpp::experimental::buffers::RingBufferImplementation> rb(3); rb.enqueue(std::make_unique(42));