From 06f89d0c98a9760446fb83f29844a2b37c2bfd6e Mon Sep 17 00:00:00 2001 From: Dirk Ziegelmeier Date: Sun, 13 Oct 2024 10:13:28 +0200 Subject: [PATCH] Fix bug in static pool Getting ALL events from pool caused SEGV --- include/cpp_event_framework/StaticPool.hxx | 5 ++++ ...ActiveObjectFrameworkEmbedded_unittest.cxx | 25 +++++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/include/cpp_event_framework/StaticPool.hxx b/include/cpp_event_framework/StaticPool.hxx index ab529f6..dcd77d7 100644 --- a/include/cpp_event_framework/StaticPool.hxx +++ b/include/cpp_event_framework/StaticPool.hxx @@ -87,6 +87,10 @@ public: auto* result = first_; first_ = result->next; + if (first_ == nullptr) + { + last_ = nullptr; + } fill_level_--; return result; } @@ -109,6 +113,7 @@ public: last_->next = ptr; } last_ = ptr; + ptr->next = nullptr; fill_level_++; assert(FillLevel() <= NumElements); diff --git a/test/ActiveObjectFrameworkEmbedded_unittest.cxx b/test/ActiveObjectFrameworkEmbedded_unittest.cxx index 41665f3..e7fb5ba 100644 --- a/test/ActiveObjectFrameworkEmbedded_unittest.cxx +++ b/test/ActiveObjectFrameworkEmbedded_unittest.cxx @@ -10,10 +10,13 @@ #include "../examples/activeobject_embedded/FsmImpl.hxx" +#include #include +#include #include #include +#include #include using namespace std::chrono_literals; @@ -35,19 +38,31 @@ void ActiveObjectFrameworkEmbeddedMain() { malloc_called = false; cpp_active_objects_embedded::EventQueue<10> queue; + + cpp_event_framework::StaticPool<3, example::activeobject_embedded::EventPoolElementSizeCalculator::kSptrSize> pool( + "EmbeddedEventPool"); + // Tell EventPoolAllocator to use pool created above + example::activeobject_embedded::EventPoolAllocator::SetAllocator(&pool); assert(!malloc_called); + // Test getting all events from queue and release them all afterwards + // Tests pool full -> empty -> full + for (int i = 0; i < 3; i++) + { + std::vector events; + for (size_t j = 0; j < pool.Size(); j++) + { + events.emplace_back(example::activeobject_embedded::Go2::MakeShared()); + } + events.clear(); + } + // SingleThreadActiveObjectDomain uses jthread - thread creation uses heap. // You need to write your own heapless SingleThreadActiveObjectDomain here if desired. cpp_active_objects_embedded::SingleThreadActiveObjectDomain domain(&queue); malloc_called = false; - cpp_event_framework::StaticPool<2, example::activeobject_embedded::EventPoolElementSizeCalculator::kSptrSize> pool( - "EmbeddedEventPool"); - // Tell EventPoolAllocator to use pool created above - example::activeobject_embedded::EventPoolAllocator::SetAllocator(&pool); - example::activeobject_embedded::FsmImpl active_object; domain.RegisterObject(&active_object);