Skip to content

Commit

Permalink
Fix font textures leaked during Rml::Shutdown due to wrong order of s…
Browse files Browse the repository at this point in the history
…hutdown calls. Log an error in debug mode if any textures leak after shutdown. See #133.
  • Loading branch information
mikke89 committed Oct 9, 2020
1 parent e103102 commit db4ee74
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
20 changes: 13 additions & 7 deletions Source/Core/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ static ContextMap contexts;

bool Initialise()
{
RMLUI_ASSERTMSG(!initialised, "Rml::Initialise() called, but RmlUi is already initialised!");

Log::Initialise();

// Check for valid interfaces, or install default interfaces as appropriate.
if (!system_interface)
{
Expand All @@ -96,8 +100,6 @@ bool Initialise()
#endif
}

Log::Initialise();

EventSpecificationInterface::Initialize();

TextureDatabase::Initialise();
Expand Down Expand Up @@ -130,29 +132,33 @@ bool Initialise()

void Shutdown()
{
RMLUI_ASSERTMSG(initialised, "Rml::Shutdown() called, but RmlUi is not initialised!");

// Clear out all contexts, which should also clean up all attached elements.
contexts.clear();

// Notify all plugins we're being shutdown.
PluginRegistry::NotifyShutdown();

Factory::Shutdown();
TemplateCache::Shutdown();
StyleSheetFactory::Shutdown();
StyleSheetSpecification::Shutdown();
TextureDatabase::Shutdown();
Factory::Shutdown();

Log::Shutdown();
font_interface = nullptr;
default_font_interface.reset();

TextureDatabase::Shutdown();

initialised = false;

render_interface = nullptr;
file_interface = nullptr;
system_interface = nullptr;
font_interface = nullptr;

default_file_interface.reset();
default_font_interface.reset();

Log::Shutdown();
}

// Returns the version of this RmlUi library.
Expand Down
18 changes: 18 additions & 0 deletions Source/Core/TextureDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ TextureDatabase::TextureDatabase()
TextureDatabase::~TextureDatabase()
{
RMLUI_ASSERT(texture_database == this);

#ifdef RMLUI_DEBUG
// All textures not owned by the database should have been released at this point.
int num_leaks_file = 0;

for (auto& texture : textures)
num_leaks_file += (texture.second.use_count() > 1);

const int num_leaks_callback = (int)callback_textures.size();
const int total_num_leaks = num_leaks_file + num_leaks_callback;

if (total_num_leaks > 0)
{
Log::Message(Log::LT_ERROR, "Textures leaked during shutdown. Total: %d From file: %d Generated: %d.",
total_num_leaks, num_leaks_file, num_leaks_callback);
}
#endif

texture_database = nullptr;
}

Expand Down

0 comments on commit db4ee74

Please sign in to comment.