ReadMostlySharedPtrTest, subtle test bug caught by -fsanitize-address-use-after-scope
authorLouis Brandy <ldbrandy@fb.com>
Sat, 8 Apr 2017 02:38:33 +0000 (19:38 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Sat, 8 Apr 2017 02:49:33 +0000 (19:49 -0700)
Summary: Because these `atomic<int>` are referenced in the destructor of the `ReadMostlyMainPtr`, they must be declared before it, otherwise they are destroyed before it i.

Reviewed By: yfeldblum

Differential Revision: D4854964

fbshipit-source-id: 625a8f890bba166b682ca13558f8bcde86edcc75

folly/experimental/test/ReadMostlySharedPtrTest.cpp

index 15f36cb8dc6ad1ebd03175956815cb599c8b282b..8b3140e997f48251c5a67ed1e40d7c9caa796cdb 100644 (file)
@@ -211,10 +211,12 @@ TEST_F(ReadMostlySharedPtrTest, Ctor) {
 }
 
 TEST_F(ReadMostlySharedPtrTest, ClearingCache) {
 }
 
 TEST_F(ReadMostlySharedPtrTest, ClearingCache) {
+  std::atomic<int> cnt1{0};
+  std::atomic<int> cnt2{0};
+
   ReadMostlyMainPtr<TestObject> ptr;
 
   // Store 1.
   ReadMostlyMainPtr<TestObject> ptr;
 
   // Store 1.
-  std::atomic<int> cnt1{0};
   ptr.reset(folly::make_unique<TestObject>(1, cnt1));
 
   Coordinator c;
   ptr.reset(folly::make_unique<TestObject>(1, cnt1));
 
   Coordinator c;
@@ -230,7 +232,6 @@ TEST_F(ReadMostlySharedPtrTest, ClearingCache) {
   EXPECT_EQ(1, cnt1.load());
 
   // Store 2 and check that 1 is destroyed.
   EXPECT_EQ(1, cnt1.load());
 
   // Store 2 and check that 1 is destroyed.
-  std::atomic<int> cnt2{0};
   ptr.reset(folly::make_unique<TestObject>(2, cnt2));
   EXPECT_EQ(0, cnt1.load());
 
   ptr.reset(folly::make_unique<TestObject>(2, cnt2));
   EXPECT_EQ(0, cnt1.load());