From c64d359b30e8960cf4a1ffa5f98730c451cfaecc 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. Test Plan: unit test Reviewed By: chip@fb.com Subscribers: njormrod FB internal diff: D1591270 --- folly/experimental/Singleton.cpp | 18 ++++++++++++++++-- folly/experimental/Singleton.h | 2 ++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/folly/experimental/Singleton.cpp b/folly/experimental/Singleton.cpp index 7934ab1a..ee4b2021 100644 --- a/folly/experimental/Singleton.cpp +++ b/folly/experimental/Singleton.cpp @@ -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; + +} + } diff --git a/folly/experimental/Singleton.h b/folly/experimental/Singleton.h index c2100e0d..cbbd0319 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