From: Andrii Grynenko Date: Thu, 2 Oct 2014 03:34:27 +0000 (-0700) Subject: Leak folly::SingletonVault X-Git-Tag: v0.22.0~303 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=c64d359b30e8960cf4a1ffa5f98730c451cfaecc;p=folly.git 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 --- 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;