Skip to content

Commit 06f89d0

Browse files
committed
Fix bug in static pool
Getting ALL events from pool caused SEGV
1 parent 382758a commit 06f89d0

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

include/cpp_event_framework/StaticPool.hxx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ public:
8787

8888
auto* result = first_;
8989
first_ = result->next;
90+
if (first_ == nullptr)
91+
{
92+
last_ = nullptr;
93+
}
9094
fill_level_--;
9195
return result;
9296
}
@@ -109,6 +113,7 @@ public:
109113
last_->next = ptr;
110114
}
111115
last_ = ptr;
116+
ptr->next = nullptr;
112117

113118
fill_level_++;
114119
assert(FillLevel() <= NumElements);

test/ActiveObjectFrameworkEmbedded_unittest.cxx

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010

1111
#include "../examples/activeobject_embedded/FsmImpl.hxx"
1212

13+
#include <cstddef>
1314
#include <iostream>
15+
#include <vector>
1416

1517
#include <cpp_active_objects_embedded/EventQueue.hxx>
1618
#include <cpp_active_objects_embedded/SingleThreadActiveObjectDomain.hxx>
19+
#include <cpp_event_framework/Signal.hxx>
1720
#include <cpp_event_framework/StaticPool.hxx>
1821

1922
using namespace std::chrono_literals;
@@ -35,19 +38,31 @@ void ActiveObjectFrameworkEmbeddedMain()
3538
{
3639
malloc_called = false;
3740
cpp_active_objects_embedded::EventQueue<10> queue;
41+
42+
cpp_event_framework::StaticPool<3, example::activeobject_embedded::EventPoolElementSizeCalculator::kSptrSize> pool(
43+
"EmbeddedEventPool");
44+
// Tell EventPoolAllocator to use pool created above
45+
example::activeobject_embedded::EventPoolAllocator::SetAllocator(&pool);
3846
assert(!malloc_called);
3947

48+
// Test getting all events from queue and release them all afterwards
49+
// Tests pool full -> empty -> full
50+
for (int i = 0; i < 3; i++)
51+
{
52+
std::vector<cpp_event_framework::Signal::SPtr> events;
53+
for (size_t j = 0; j < pool.Size(); j++)
54+
{
55+
events.emplace_back(example::activeobject_embedded::Go2::MakeShared());
56+
}
57+
events.clear();
58+
}
59+
4060
// SingleThreadActiveObjectDomain uses jthread - thread creation uses heap.
4161
// You need to write your own heapless SingleThreadActiveObjectDomain here if desired.
4262
cpp_active_objects_embedded::SingleThreadActiveObjectDomain domain(&queue);
4363

4464
malloc_called = false;
4565

46-
cpp_event_framework::StaticPool<2, example::activeobject_embedded::EventPoolElementSizeCalculator::kSptrSize> pool(
47-
"EmbeddedEventPool");
48-
// Tell EventPoolAllocator to use pool created above
49-
example::activeobject_embedded::EventPoolAllocator::SetAllocator(&pool);
50-
5166
example::activeobject_embedded::FsmImpl active_object;
5267
domain.RegisterObject(&active_object);
5368

0 commit comments

Comments
 (0)