This is an automated email from the ASF dual-hosted git repository.

guangmingchen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brpc.git


The following commit(s) were added to refs/heads/master by this push:
     new b4d4acb7 [MPSQ] fix a memleak in butil::ObjectPoolAllocator (#2725)
b4d4acb7 is described below

commit b4d4acb7cd9a677039f662f18df37d4be7172ed3
Author: David Lee <live4t...@gmail.com>
AuthorDate: Tue Aug 6 14:26:55 2024 +0800

    [MPSQ] fix a memleak in butil::ObjectPoolAllocator (#2725)
    
    Without specifying the type of the pointer, the memory will return to
    the pool holding all `void' pointers.
    
    Signed-off-by: Qun, Li <qun...@zstack.io>
---
 src/butil/containers/mpsc_queue.h |  2 +-
 test/mpsc_queue_unittest.cc       | 22 +++++++++++++++++++++-
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/src/butil/containers/mpsc_queue.h 
b/src/butil/containers/mpsc_queue.h
index 45ab2ebe..f9e1d780 100644
--- a/src/butil/containers/mpsc_queue.h
+++ b/src/butil/containers/mpsc_queue.h
@@ -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)); }
 };
 
 
diff --git a/test/mpsc_queue_unittest.cc b/test/mpsc_queue_unittest.cc
index bd152a5d..f57a85f6 100644
--- a/test/mpsc_queue_unittest.cc
+++ b/test/mpsc_queue_unittest.cc
@@ -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;
@@ -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);
 
-}
\ No newline at end of file
+    p = alloc.Alloc();
+    info = butil::describe_objects<butil::MPSCQueueNode<MyObject>>();
+    ASSERT_EQ(1, info.item_num);
+}
+
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org
For additional commands, e-mail: dev-h...@brpc.apache.org

Reply via email to