Commit ad1889b2 authored by Nick Terrell's avatar Nick Terrell Committed by Facebook Github Bot

Use annotate_object_leaked() in LeakySingleton

Summary:
Use `folly::annotate_object_leaked()` in `folly::LeakySingleton`
instead of its own custom suppression. Additionally, this removes
the overhead of the `std::list<>` in opt-mode, but this is minor.

Reviewed By: yfeldblum

Differential Revision: D15755787

fbshipit-source-id: 053b8789c58941176977e6adbc4863d33d6ebed9
parent eb02c609
...@@ -130,6 +130,7 @@ ...@@ -130,6 +130,7 @@
#include <folly/experimental/ReadMostlySharedPtr.h> #include <folly/experimental/ReadMostlySharedPtr.h>
#include <folly/hash/Hash.h> #include <folly/hash/Hash.h>
#include <folly/lang/Exception.h> #include <folly/lang/Exception.h>
#include <folly/memory/SanitizeLeak.h>
#include <folly/synchronization/Baton.h> #include <folly/synchronization/Baton.h>
#include <folly/synchronization/RWSpinLock.h> #include <folly/synchronization/RWSpinLock.h>
...@@ -717,8 +718,7 @@ class LeakySingleton { ...@@ -717,8 +718,7 @@ class LeakySingleton {
auto& entry = entryInstance(); auto& entry = entryInstance();
if (entry.ptr) { if (entry.ptr) {
// Make sure existing pointer doesn't get reported as a leak by LSAN. annotate_object_leaked(std::exchange(entry.ptr, nullptr));
entry.leakedPtrs.push_back(std::exchange(entry.ptr, nullptr));
} }
entry.createFunc = createFunc; entry.createFunc = createFunc;
entry.state = State::Dead; entry.state = State::Dead;
...@@ -737,7 +737,6 @@ class LeakySingleton { ...@@ -737,7 +737,6 @@ class LeakySingleton {
CreateFunc createFunc; CreateFunc createFunc;
std::mutex mutex; std::mutex mutex;
detail::TypeDescriptor type_{typeid(T), typeid(Tag)}; detail::TypeDescriptor type_{typeid(T), typeid(Tag)};
std::list<T*> leakedPtrs;
}; };
static Entry& entryInstance() { static Entry& entryInstance() {
......
...@@ -783,3 +783,14 @@ TEST(Singleton, DoubleMakeMockAfterTryGet) { ...@@ -783,3 +783,14 @@ TEST(Singleton, DoubleMakeMockAfterTryGet) {
EXPECT_EQ(1, counts.ctor); EXPECT_EQ(1, counts.ctor);
EXPECT_EQ(1, counts.dtor); EXPECT_EQ(1, counts.dtor);
} }
TEST(Singleton, LeakySingletonLSAN) {
struct PrivateTag {};
static folly::LeakySingleton<int, PrivateTag> gPtr;
auto* ptr0 = &gPtr.get();
EXPECT_EQ(*ptr0, 0);
gPtr.make_mock([] { return new int(1); });
auto* ptr1 = &gPtr.get();
EXPECT_NE(ptr0, ptr1);
EXPECT_EQ(*ptr1, 1);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment