Skip to content

Commit

Permalink
fix: missing destructor call in Pistache::Queue
Browse files Browse the repository at this point in the history
  • Loading branch information
tyler92 committed Oct 15, 2024
1 parent 6e3f769 commit 52c24eb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
1 change: 1 addition & 0 deletions include/pistache/mailbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ namespace Pistache
// Since it's Single-Consumer, the store does not need to be atomic
tail = next;
new (&res->storage) T(std::move(next->data()));
next->data().~T();
return res;
}
return nullptr;
Expand Down
48 changes: 43 additions & 5 deletions tests/mailbox_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,47 @@

struct Data
{
static int num_instances;
static inline int num_instances = 0;
static constexpr int fingerprint = 0xdeadbeef;

Data()
: val(Data::fingerprint)
, payload(std::string(100, 'x'))
{
num_instances++;
}

Data(Data&&)
: val(Data::fingerprint)
, payload(std::string(100, 'x'))
{
num_instances++;
}

Data(const Data&) = delete;

~Data()
{
EXPECT_EQ(val, Data::fingerprint);
EXPECT_GE(0, --num_instances);
EXPECT_GE(--num_instances, 0);
}

int val;

// Dynamic allocation is required to detect a potential memory leak here
std::string payload;
};

int Data::num_instances = 0;
constexpr int Data::fingerprint;
class QueueTest : public testing::Test
{
public:
void SetUp() override
{
Data::num_instances = 0;
}
};

TEST(queue_test, destructor_test)
TEST_F(QueueTest, destructor_test)
{
Pistache::Queue<Data> queue;
EXPECT_TRUE(queue.empty());
Expand All @@ -41,3 +60,22 @@ TEST(queue_test, destructor_test)
}
// Should call Data::~Data 5 times and not 6 (placeholder entry)
}

TEST_F(QueueTest, push_pop)
{
auto queue = std::make_unique<Pistache::Queue<Data>>();
EXPECT_TRUE(queue->empty());

for (int i = 0; i < 5; i++)
{
queue->push(Data());
}

for (int i = 0; i < 5; i++)
{
EXPECT_NE(queue->popSafe(), nullptr);
}

EXPECT_TRUE(queue->empty());
EXPECT_EQ(Data::num_instances, 0);
}
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.8.20241014
0.4.8.20241015

0 comments on commit 52c24eb

Please sign in to comment.