Skip to content

Commit

Permalink
[MPSQ] fix a memleak in butil::ObjectPoolAllocator (#2725)
Browse files Browse the repository at this point in the history
Without specifying the type of the pointer, the memory will return to
the pool holding all `void' pointers.

Signed-off-by: Qun, Li <qun.li@zstack.io>
  • Loading branch information
live4thee authored Aug 6, 2024
1 parent 64ce760 commit b4d4acb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/butil/containers/mpsc_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ template <typename T>
class ObjectPoolAllocator {
public:
void* Alloc() { return get_object<MPSCQueueNode<T>>(); }
void Free(void* p) { return_object(p); }
void Free(void* p) { return_object(static_cast<MPSCQueueNode<T>*>(p)); }
};


Expand Down
22 changes: 21 additions & 1 deletion test/mpsc_queue_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#include <pthread.h>
#include "butil/containers/mpsc_queue.h"

#define BAIDU_CLEAR_OBJECT_POOL_AFTER_ALL_THREADS_QUIT
#include "butil/object_pool.h"

namespace {

const uint MAX_COUNT = 1000000;
Expand Down Expand Up @@ -120,5 +123,22 @@ TEST(MPSCQueueTest, mpsc_multi_thread) {

}

struct MyObject {};

TEST(MPSCQueueTest, mpsc_test_allocator) {
butil::ObjectPoolAllocator<MyObject> alloc;

auto p = alloc.Alloc();
butil::ObjectPoolInfo info = butil::describe_objects<butil::MPSCQueueNode<MyObject>>();
ASSERT_EQ(1, info.item_num);

alloc.Free(p);
info = butil::describe_objects<butil::MPSCQueueNode<MyObject>>();
ASSERT_EQ(1, info.item_num);

}
p = alloc.Alloc();
info = butil::describe_objects<butil::MPSCQueueNode<MyObject>>();
ASSERT_EQ(1, info.item_num);
}

}

0 comments on commit b4d4acb

Please sign in to comment.