From 5a13e4eda27f100e4714c08ae030886e1c71317d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 13 Nov 2017 21:09:48 +0100 Subject: [PATCH] Clear wxFontEnumerator face names cache on library shutdown Don't rely on it being done during statics cleanup as this doesn't work if the library is shutdown and re-initialized. Use a module to do the cleanup, just as it's already done for a lot of other global data in wx. --- src/common/fontenumcmn.cpp | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/common/fontenumcmn.cpp b/src/common/fontenumcmn.cpp index d0df597226..a1a1bbf771 100644 --- a/src/common/fontenumcmn.cpp +++ b/src/common/fontenumcmn.cpp @@ -26,6 +26,31 @@ #if wxUSE_FONTENUM #include "wx/fontenum.h" +#include "wx/module.h" + +namespace +{ + +// Cached result of GetFacenames(). +wxArrayString gs_allFacenames; + +// Module used to ensure the cache is cleared on library shutdown and so is not +// reused if it re-initialized again later. +class wxFontEnumCacheCleanupModule : public wxModule +{ +public: + wxFontEnumCacheCleanupModule() { } + + bool OnInit() wxOVERRIDE { return true; } + void OnExit() wxOVERRIDE { gs_allFacenames.clear(); } + +private: + wxDECLARE_DYNAMIC_CLASS(wxFontEnumCacheCleanupModule); +}; + +wxIMPLEMENT_DYNAMIC_CLASS(wxFontEnumCacheCleanupModule, wxModule); + +} // anonymous namespace // ============================================================================ // implementation @@ -79,7 +104,8 @@ bool wxFontEnumerator::IsValidFacename(const wxString &facename) { // we cache the result of wxFontEnumerator::GetFacenames supposing that // the array of face names won't change in the session of this program - static wxArrayString s_arr = wxFontEnumerator::GetFacenames(); + if ( gs_allFacenames.empty() ) + gs_allFacenames = wxFontEnumerator::GetFacenames(); #ifdef __WXMSW__ // Quoting the MSDN: @@ -95,7 +121,7 @@ bool wxFontEnumerator::IsValidFacename(const wxString &facename) #endif // is given font face name a valid one ? - if (s_arr.Index(facename, false) == wxNOT_FOUND) + if (gs_allFacenames.Index(facename, false) == wxNOT_FOUND) return false; return true;