fix leak in MPMCQueueTest's use of boost::intrusive_ptr
authorNathan Bronson <ngbronson@fb.com>
Mon, 22 Sep 2014 18:33:28 +0000 (11:33 -0700)
committerAnton Likhtarov <alikhtarov@fb.com>
Fri, 26 Sep 2014 22:20:19 +0000 (15:20 -0700)
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

index ea0fbd123d81dd10548f5684e378f0898036abb7..1a84b485aba2cc7f78d9574643169abf6bc58e4a 100644 (file)
@@ -100,17 +100,27 @@ void runElementTypeTest(T&& src) {
 }
 
 struct RefCounted {
+  static __thread int active_instances;
+
   mutable std::atomic<int> 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<char>('a'));
   runElementTypeTest(folly::make_unique<char>('a'));
   runElementTypeTest(boost::intrusive_ptr<RefCounted>(new RefCounted));
+  EXPECT_EQ(RefCounted::active_instances, 0);
 }
 
 TEST(MPMCQueue, single_thread_enqdeq) {