[Bf-blender-cvs] [9b2661a8e07] ui-asset-view-template: Construct-on-first-use to stay far away from static init. fiasco

Julian Eisel noreply at git.blender.org
Tue Mar 16 17:42:51 CET 2021


Commit: 9b2661a8e072c4ddac317390ca47a3b43ae26496
Author: Julian Eisel
Date:   Tue Mar 16 17:38:00 2021 +0100
Branches: ui-asset-view-template
https://developer.blender.org/rB9b2661a8e072c4ddac317390ca47a3b43ae26496

Construct-on-first-use to stay far away from static init. fiasco

We didn't actually run into the static initialization fiasco, but only
because `blender::Map` didn't use the guarded allocator on default
destruction. Better to stay far way from static initialization fiasco,
and the construct on first use idiom is trivial and prevents it. So
better use that.

===================================================================

M	source/blender/editors/asset/asset_list.cc

===================================================================

diff --git a/source/blender/editors/asset/asset_list.cc b/source/blender/editors/asset/asset_list.cc
index 2ff7aa48edd..641304d6ff1 100644
--- a/source/blender/editors/asset/asset_list.cc
+++ b/source/blender/editors/asset/asset_list.cc
@@ -285,7 +285,6 @@ class AssetList {
  */
 class AssetListStorage {
   using AssetListMap = Map<AssetLibraryReferenceWrapper, AssetList>;
-  static AssetListMap global_storage_;
 
  public:
   static void fetch_library(const AssetLibraryReference &library_reference,
@@ -308,24 +307,24 @@ class AssetListStorage {
 
   static void destruct()
   {
-    global_storage_.~AssetListMap();
+    global_storage().~AssetListMap();
   }
 
   static AssetList *lookup_list(const AssetLibraryReference &library_ref)
   {
-    return global_storage_.lookup_ptr(library_ref);
+    return global_storage().lookup_ptr(library_ref);
   }
 
   static void tagMainDataDirty()
   {
-    for (AssetList &list : global_storage_.values()) {
+    for (AssetList &list : global_storage().values()) {
       list.tagMainDataDirty();
     }
   }
 
   static void remapID(ID *id_new, ID *id_old)
   {
-    for (AssetList &list : global_storage_.values()) {
+    for (AssetList &list : global_storage().values()) {
       list.remapID(id_new, id_old);
     }
   }
@@ -351,15 +350,24 @@ class AssetListStorage {
   static std::tuple<AssetList &, is_new_t> ensure_list_storage(
       const AssetLibraryReference &library_reference, eFileSelectType filesel_type)
   {
-    if (AssetList *list = global_storage_.lookup_ptr(library_reference)) {
+    AssetListMap &storage = global_storage();
+
+    if (AssetList *list = storage.lookup_ptr(library_reference)) {
       return {*list, false};
     }
-    global_storage_.add(library_reference, AssetList(filesel_type, library_reference));
-    return {global_storage_.lookup(library_reference), true};
+    storage.add(library_reference, AssetList(filesel_type, library_reference));
+    return {storage.lookup(library_reference), true};
   }
-};
 
-AssetListStorage::AssetListMap AssetListStorage::global_storage_{};
+  /**
+   * Wrapper for Construct on First Use idiom, to avoid the Static Initialization Fiasco.
+   */
+  static AssetListMap &global_storage()
+  {
+    static AssetListMap global_storage_;
+    return global_storage_;
+  }
+};
 
 /** \} */



More information about the Bf-blender-cvs mailing list