Skip to content

Commit

Permalink
Manually control lifetime of observer pointer data
Browse files Browse the repository at this point in the history
This avoids a potential issue with global shutdown order. The controlled lifetime resource itself could previously be destroyed before all observer pointers referring to its resources were destroyed.
  • Loading branch information
mikke89 committed Oct 20, 2024
1 parent 4120031 commit 3c10d0f
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions Source/Core/ObserverPtr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
*/

#include "../../Include/RmlUi/Core/ObserverPtr.h"
#include "../../Include/RmlUi/Core/Log.h"
#include "ControlledLifetimeResource.h"
#include "Pool.h"

namespace Rml {
Expand All @@ -37,7 +35,7 @@ struct ObserverPtrData {
bool is_shutdown = false;
Pool<Detail::ObserverPtrBlock> block_pool{128, true};
};
static ControlledLifetimeResource<ObserverPtrData> observer_ptr_data;
static ObserverPtrData* observer_ptr_data = nullptr;

void Detail::DeallocateObserverPtrBlockIfEmpty(ObserverPtrBlock* block)
{
Expand All @@ -47,22 +45,25 @@ void Detail::DeallocateObserverPtrBlockIfEmpty(ObserverPtrBlock* block)
observer_ptr_data->block_pool.DestroyAndDeallocate(block);
if (observer_ptr_data->is_shutdown && observer_ptr_data->block_pool.GetNumAllocatedObjects() == 0)
{
observer_ptr_data.Shutdown();
delete observer_ptr_data;
observer_ptr_data = nullptr;
}
}
}

void Detail::InitializeObserverPtrPool()
{
observer_ptr_data.InitializeIfEmpty();
if (!observer_ptr_data)
observer_ptr_data = new ObserverPtrData;
observer_ptr_data->is_shutdown = false;
}

void Detail::ShutdownObserverPtrPool()
{
const int num_objects = observer_ptr_data->block_pool.GetNumAllocatedObjects();
if (num_objects == 0)
if (observer_ptr_data->block_pool.GetNumAllocatedObjects() == 0)
{
observer_ptr_data.Shutdown();
delete observer_ptr_data;
observer_ptr_data = nullptr;
}
else
{
Expand Down

0 comments on commit 3c10d0f

Please sign in to comment.