Fix destruction order problem in getCoreAllocator
authorGiuseppe Ottaviano <ott@fb.com>
Wed, 7 Jun 2017 00:32:12 +0000 (17:32 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Wed, 7 Jun 2017 00:35:42 +0000 (17:35 -0700)
Summary: We cannot guarantee the correct destruction order of the Meyers singleton owning the allocator, and the objects allocated with it, so we just leak it using `Indestructible`.

Reviewed By: djwatson

Differential Revision: D5196227

fbshipit-source-id: ec07ab1e21af7814194883b252d45aa36d2a04b1

folly/detail/CacheLocality.h

index b6dd66e7808d6c3837d86c427cf9fa681fe66bdf..b7aa2553219db205e950c2672205451f64e59876 100644 (file)
@@ -29,6 +29,7 @@
 #include <vector>
 
 #include <folly/Hash.h>
+#include <folly/Indestructible.h>
 #include <folly/Likely.h>
 #include <folly/Memory.h>
 #include <folly/Portability.h>
@@ -492,8 +493,10 @@ class CoreAllocator {
 
 template <size_t Stripes>
 typename CoreAllocator<Stripes>::Allocator* getCoreAllocator(size_t stripe) {
-  static CoreAllocator<Stripes> allocator;
-  return allocator.get(stripe);
+  // We cannot make sure that the allocator will be destroyed after
+  // all the objects allocated with it, so we leak it.
+  static Indestructible<CoreAllocator<Stripes>> allocator;
+  return allocator->get(stripe);
 }
 
 template <typename T, size_t Stripes>