Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: missing destructor call in Pistache::Queue #1255

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.9.20241016
0.4.10.20241017
Loading