Leak folly::SingletonVault
authorAndrii Grynenko <andrii@fb.com>
Thu, 2 Oct 2014 03:34:27 +0000 (20:34 -0700)
committerAndrii Grynenko <andrii@fb.com>
Wed, 15 Oct 2014 00:48:20 +0000 (17:48 -0700)
Summary: This will ensure SingletonVault is always available. It matters for singletons, not managed by folly::Singleton. Singletons in it will actually be destroyed via static SingletonVaultDestructor.

Test Plan: unit test

Reviewed By: chip@fb.com

Subscribers: njormrod

FB internal diff: D1591270

folly/experimental/Singleton.cpp
folly/experimental/Singleton.h

index 7934ab1a8a7a0b1804e49a81c1f7e53b65fa0c6d..ee4b202164ade72fa40705ac202576c7e0153543 100644 (file)
@@ -55,7 +55,21 @@ void SingletonVault::destroyInstances() {
 }
 
 SingletonVault* SingletonVault::singleton() {
-  static SingletonVault vault;
-  return &vault;
+  static SingletonVault* vault = new SingletonVault();
+  return vault;
 }
+
+namespace {
+
+class SingletonVaultDestructor {
+ public:
+  ~SingletonVaultDestructor() {
+    SingletonVault::singleton()->destroyInstances();
+  }
+};
+
+SingletonVaultDestructor singletonVaultDestructor;
+
+}
+
 }
index c2100e0d90773a7009b75d8fbc5eb67d0c1831f6..cbbd03199503b4b281461b86130550836f27ae36 100644 (file)
@@ -167,6 +167,8 @@ class SingletonVault {
   enum class Type { Strict, Relaxed };
 
   explicit SingletonVault(Type type = Type::Relaxed) : type_(type) {}
+
+  // Destructor is only called by unit tests to check destroyInstances.
   ~SingletonVault();
 
   typedef std::function<void(void*)> TeardownFunc;