Skip to content

Commit

Permalink
Add ReleaseMemoryPools to free up any memory pools and free the Obser…
Browse files Browse the repository at this point in the history
…verPtrBlock pool. (#265)
  • Loading branch information
jack9267 authored Jan 15, 2022
1 parent f1eba19 commit ab90eea
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Include/RmlUi/Core/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ RMLUICORE_API void ReleaseTextures(RenderInterface* render_interface = nullptr);
/// Forces all compiled geometry handles generated by RmlUi to be released.
RMLUICORE_API void ReleaseCompiledGeometry();

/// Forces all memory pools used by RmlUi to be released.
RMLUICORE_API void ReleaseMemoryPools();

} // namespace Rml

#endif
17 changes: 17 additions & 0 deletions Source/Core/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
#include "../SVG/SVGPlugin.h"
#endif

#include "Pool.h"


namespace Rml {

Expand All @@ -80,6 +82,9 @@ static bool initialised = false;
using ContextMap = UnorderedMap< String, ContextPtr >;
static ContextMap contexts;

// The ObserverPtrBlock pool
extern Pool<ObserverPtrBlock>* observerPtrBlockPool;

#ifndef RMLUI_VERSION
#define RMLUI_VERSION "custom"
#endif
Expand Down Expand Up @@ -178,6 +183,9 @@ void Shutdown()
default_file_interface.reset();

Log::Shutdown();

// Release any memory pools
ReleaseMemoryPools();
}

// Returns the version of this RmlUi library.
Expand Down Expand Up @@ -371,4 +379,13 @@ void ReleaseCompiledGeometry()
return GeometryDatabase::ReleaseAll();
}

void ReleaseMemoryPools()
{
if (observerPtrBlockPool && observerPtrBlockPool->GetNumAllocatedObjects() <= 0)
{
delete observerPtrBlockPool;
observerPtrBlockPool = nullptr;
}
}

} // namespace Rml
13 changes: 9 additions & 4 deletions Source/Core/ObserverPtr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,22 @@

namespace Rml {

static Pool< ObserverPtrBlock >& GetPool()
// The ObserverPtrBlock pool
Pool<ObserverPtrBlock>* observerPtrBlockPool = nullptr;

static Pool<ObserverPtrBlock>& GetPool()
{
// Wrap pool in a function to ensure it is initialized before use.
// This pool must outlive all other global variables that derive from EnableObserverPtr. This even includes
// user variables which we have no control over. For this reason, we intentionally let this leak.
static Pool< ObserverPtrBlock >* pool = new Pool< ObserverPtrBlock >(128, true);
return *pool;
if (observerPtrBlockPool == nullptr)
observerPtrBlockPool = new Pool<ObserverPtrBlock>(128, true);
return *observerPtrBlockPool;
}


void DeallocateObserverPtrBlockIfEmpty(ObserverPtrBlock* block) {
void DeallocateObserverPtrBlockIfEmpty(ObserverPtrBlock* block)
{
RMLUI_ASSERT(block->num_observers >= 0);
if (block->num_observers == 0 && block->pointed_to_object == nullptr)
{
Expand Down

0 comments on commit ab90eea

Please sign in to comment.