Browse Source

Ensure initialization of observer pointer pool before use

Michael Ragazzon 6 years ago
parent
commit
8d58625727
2 changed files with 9 additions and 3 deletions
  1. 1 0
      Include/RmlUi/Core/ObserverPtr.h
  2. 8 3
      Source/Core/ObserverPtr.cpp

+ 1 - 0
Include/RmlUi/Core/ObserverPtr.h

@@ -69,6 +69,7 @@ template<typename T>
 class RMLUICORE_API ObserverPtr {
 public:
 	ObserverPtr() noexcept : block(nullptr) {}
+	ObserverPtr(std::nullptr_t) noexcept : block(nullptr) {}
 	~ObserverPtr() noexcept {
 		reset(); 
 	}

+ 8 - 3
Source/Core/ObserverPtr.cpp

@@ -34,20 +34,25 @@ namespace Rml {
 namespace Core {
 
 
-static Pool< ObserverPtrBlock > observer_ptr_block_pool(400, true);
+static Pool< ObserverPtrBlock >& GetPool()
+{
+	// Wrap pool in a function to ensure it is initialized before use.
+	static Pool< ObserverPtrBlock > pool(400, true);
+	return pool;
+}
 
 
 void DeallocateObserverPtrBlockIfEmpty(ObserverPtrBlock* block) {
 	RMLUI_ASSERT(block->num_observers >= 0);
 	if (block->num_observers == 0 && block->pointed_to_object == nullptr)
 	{
-		observer_ptr_block_pool.DestroyAndDeallocate(block);
+		GetPool().DestroyAndDeallocate(block);
 	}
 }
 
 ObserverPtrBlock* AllocateObserverPtrBlock()
 {
-	return observer_ptr_block_pool.AllocateAndConstruct();
+	return GetPool().AllocateAndConstruct();
 }