From 54ab6858b79c1bea35e5dcba593b60d4f92b58f3 Mon Sep 17 00:00:00 2001 From: Nathan Bronson Date: Mon, 22 Sep 2014 11:33:28 -0700 Subject: [PATCH] fix leak in MPMCQueueTest's use of boost::intrusive_ptr Summary: MPMCQueueTest's intrusive reference count test implementation was incorrect, so it simultaneously didn't test what it should and had a leak. Test Plan: tests, plus new __thread to track lifetimes Reviewed By: meyering@fb.com Subscribers: njormrod, soren, meyering FB internal diff: D1569448 --- folly/test/MPMCQueueTest.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/folly/test/MPMCQueueTest.cpp b/folly/test/MPMCQueueTest.cpp index ea0fbd12..1a84b485 100644 --- a/folly/test/MPMCQueueTest.cpp +++ b/folly/test/MPMCQueueTest.cpp @@ -100,17 +100,27 @@ void runElementTypeTest(T&& src) { } struct RefCounted { + static __thread int active_instances; + mutable std::atomic rc; - RefCounted() : rc(0) {} + RefCounted() : rc(0) { + ++active_instances; + } + + ~RefCounted() { + --active_instances; + } }; +__thread int RefCounted::active_instances; + void intrusive_ptr_add_ref(RefCounted const* p) { p->rc++; } void intrusive_ptr_release(RefCounted const* p) { - if (--(p->rc)) { + if (--(p->rc) == 0) { delete p; } } @@ -123,6 +133,7 @@ TEST(MPMCQueue, lots_of_element_types) { runElementTypeTest(std::make_shared('a')); runElementTypeTest(folly::make_unique('a')); runElementTypeTest(boost::intrusive_ptr(new RefCounted)); + EXPECT_EQ(RefCounted::active_instances, 0); } TEST(MPMCQueue, single_thread_enqdeq) { -- 2.34.1