From f0c0e8d28099e9d88b9f23d72f2df213f349eb5e Mon Sep 17 00:00:00 2001 From: Andrii Grynenko Date: Wed, 1 Oct 2014 20:34:27 -0700 Subject: [PATCH] Leak folly::SingletonVault 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. This does the same as D1591270 (which got reverted), but doesn't change destruction order. Test Plan: Run tests which were broken by D1591270 Reviewed By: chip@fb.com Subscribers: njormrod FB internal diff: D1594898 --- folly/experimental/Singleton.cpp | 14 ++++++++++++-- folly/experimental/Singleton.h | 2 ++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/folly/experimental/Singleton.cpp b/folly/experimental/Singleton.cpp index 7934ab1a..4cfacd5a 100644 --- a/folly/experimental/Singleton.cpp +++ b/folly/experimental/Singleton.cpp @@ -55,7 +55,17 @@ void SingletonVault::destroyInstances() { } SingletonVault* SingletonVault::singleton() { - static SingletonVault vault; - return &vault; + static SingletonVault* vault = new SingletonVault(); + + class SingletonVaultDestructor { + public: + ~SingletonVaultDestructor() { + SingletonVault::singleton()->destroyInstances(); + } + }; + static SingletonVaultDestructor singletonVaultDestructor; + + return vault; } + } diff --git a/folly/experimental/Singleton.h b/folly/experimental/Singleton.h index 524e5217..ed912abe 100644 --- a/folly/experimental/Singleton.h +++ b/folly/experimental/Singleton.h @@ -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 TeardownFunc; -- 2.34.1