0

I came across this feature from folly https://github.com/facebook/folly/blob/main/folly/Indestructible.h#L51

*  This is designed as an alternative, but with one fewer allocation at
 *  construction time and one fewer pointer dereference at access time, to the
 *  Meyers singleton pattern of:
 *
 *    void doSomethingWithExpensiveData() {
 *      static const auto data =  // never `delete`d
 *          new map<string, int>{{"key1", 17}, {"key2", 19}, {"key3", 23}};
 *      callSomethingTakingAMapByRef(*data);
 *    }

What is a valid use case for this feature? Will this cause a memory leak since it's never freed?

Zack
  • 1,205
  • 2
  • 14
  • 38
  • The Meyer's Singleton is useful to avoid the "static construction race fiasco" which occurs with global variables; global variables are not necessarily initialised in a deterministic order. Meyer's Singletons are initialised when they are first used. As with all statics, they are freed at program destruction (not in a deterministic fashion - one reason why people don't like using Singletons) – Den-Jason Mar 10 '23 at 23:52
  • This is Pointless Use Of Pointers. Using `new` accomplishes absolutely nothing at all, here, whatsoever. Having said that, the impacts are quite minor. – Sam Varshavchik Mar 11 '23 at 00:05

0 Answers0