[Bf-blender-cvs] [77df7c61e68] functions: Destruct lazy initialized objects when Blender quits

Jacques Lucke noreply at git.blender.org
Tue Jun 4 18:14:48 CEST 2019


Commit: 77df7c61e6820de0a851aed16d91921d93420464
Author: Jacques Lucke
Date:   Tue Jun 4 17:38:05 2019 +0200
Branches: functions
https://developer.blender.org/rB77df7c61e6820de0a851aed16d91921d93420464

Destruct lazy initialized objects when Blender quits

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

A	source/blender/blenlib/BLI_lazy_init.h
M	source/blender/blenlib/BLI_lazy_init.hpp
M	source/blender/blenlib/BLI_small_stack.hpp
M	source/blender/blenlib/BLI_small_vector.hpp
M	source/blender/blenlib/CMakeLists.txt
A	source/blender/blenlib/intern/BLI_lazy_init.cpp
M	source/blender/windowmanager/intern/wm_init_exit.c

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

diff --git a/source/blender/blenlib/BLI_lazy_init.h b/source/blender/blenlib/BLI_lazy_init.h
new file mode 100644
index 00000000000..a3a626202a7
--- /dev/null
+++ b/source/blender/blenlib/BLI_lazy_init.h
@@ -0,0 +1,9 @@
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void BLI_lazy_init_free_all(void);
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/source/blender/blenlib/BLI_lazy_init.hpp b/source/blender/blenlib/BLI_lazy_init.hpp
index ae926b3cc4b..74e4a09f189 100644
--- a/source/blender/blenlib/BLI_lazy_init.hpp
+++ b/source/blender/blenlib/BLI_lazy_init.hpp
@@ -1,16 +1,28 @@
 #pragma once
 
 /* These macros help to define functions that initialize
- * some data the first time it is used.
- * Currently the memory is freed after Blender prints information
- * about not-freed blocks, which are false positive memory leaks.
- * I'm not sure what the best solution to this is, yet. */
+ * some data the first time it is used. */
+
+#include <functional>
+#include <BLI_optional.hpp>
+
+namespace BLI {
+
+void register_lazy_init_free_func(std::function<void()> free_func);
+
+}  // namespace BLI
 
 #define LAZY_INIT__NO_ARG(final_ret_type, builder_ret_type, func_name) \
   static builder_ret_type func_name##_impl(void); \
+  static builder_ret_type &func_name##_builder(void) \
+  { \
+    static BLI::Optional<builder_ret_type> value = func_name##_impl(); \
+    BLI::register_lazy_init_free_func([]() { value.reset(); }); \
+    return value.value(); \
+  } \
   final_ret_type func_name(void) \
   { \
-    static builder_ret_type value = func_name##_impl(); \
+    static builder_ret_type &value = func_name##_builder(); \
     return value; \
   } \
   builder_ret_type func_name##_impl(void)
diff --git a/source/blender/blenlib/BLI_small_stack.hpp b/source/blender/blenlib/BLI_small_stack.hpp
index 60a3713c0b6..202bf78b29d 100644
--- a/source/blender/blenlib/BLI_small_stack.hpp
+++ b/source/blender/blenlib/BLI_small_stack.hpp
@@ -55,6 +55,16 @@ template<typename T, uint N = 4> class SmallStack {
   {
     return m_elements.end();
   }
+
+  void clear()
+  {
+    m_elements.clear();
+  }
+
+  void clear_and_make_small()
+  {
+    m_elements.clear_and_make_small();
+  }
 };
 
 } /* namespace BLI */
diff --git a/source/blender/blenlib/BLI_small_vector.hpp b/source/blender/blenlib/BLI_small_vector.hpp
index 9336f564e6f..5cc14347d56 100644
--- a/source/blender/blenlib/BLI_small_vector.hpp
+++ b/source/blender/blenlib/BLI_small_vector.hpp
@@ -104,7 +104,14 @@ template<typename T, uint N = 4> class SmallVector {
   void clear()
   {
     this->destruct_elements_but_keep_memory();
-    this->m_size = 0;
+    m_size = 0;
+  }
+
+  void clear_and_make_small()
+  {
+    this->destruct_elements_and_free_memory();
+    m_size = 0;
+    m_elements = this->small_buffer();
   }
 
   void append(const T &value)
diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt
index 5eef0fe93bd..8a36905f0c8 100644
--- a/source/blender/blenlib/CMakeLists.txt
+++ b/source/blender/blenlib/CMakeLists.txt
@@ -238,7 +238,9 @@ set(SRC
   BLI_array_ref.hpp
   BLI_chained_strings.hpp
   BLI_composition.hpp
+  BLI_lazy_init.h
   BLI_lazy_init.hpp
+  intern/BLI_lazy_init.cpp
   BLI_listbase_wrapper.hpp
   BLI_mempool.hpp
   BLI_multimap.hpp
diff --git a/source/blender/blenlib/intern/BLI_lazy_init.cpp b/source/blender/blenlib/intern/BLI_lazy_init.cpp
new file mode 100644
index 00000000000..6e7de60e0b7
--- /dev/null
+++ b/source/blender/blenlib/intern/BLI_lazy_init.cpp
@@ -0,0 +1,24 @@
+#include "BLI_lazy_init.hpp"
+#include "BLI_lazy_init.h"
+#include "BLI_small_stack.hpp"
+
+typedef std::function<void()> FreeFunc;
+BLI::SmallStack<FreeFunc> free_functions;
+
+void BLI_lazy_init_free_all()
+{
+  while (!free_functions.empty()) {
+    FreeFunc free_object = free_functions.pop();
+    free_object();
+  }
+  free_functions.clear_and_make_small();
+}
+
+namespace BLI {
+
+void register_lazy_init_free_func(std::function<void()> free_func)
+{
+  free_functions.push(free_func);
+}
+
+}  // namespace BLI
diff --git a/source/blender/windowmanager/intern/wm_init_exit.c b/source/blender/windowmanager/intern/wm_init_exit.c
index bbaaab01b18..fa7b3eb7c19 100644
--- a/source/blender/windowmanager/intern/wm_init_exit.c
+++ b/source/blender/windowmanager/intern/wm_init_exit.c
@@ -48,6 +48,7 @@
 #include "BLI_threads.h"
 #include "BLI_utildefines.h"
 #include "BLI_timer.h"
+#include "BLI_lazy_init.h"
 
 #include "BLO_writefile.h"
 #include "BLO_undofile.h"
@@ -486,6 +487,7 @@ void WM_exit_ext(bContext *C, const bool do_python)
   }
 
   BLI_timer_free();
+  BLI_lazy_init_free_all();
 
   WM_paneltype_clear();



More information about the Bf-blender-cvs mailing list