From 01302829870e2c5b10636f853791d9f7154684ef Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 29 Jan 2023 17:14:11 +0000 Subject: [PATCH] Create wxPGGlobalVars on demand and not on startup This is slightly more efficient as it avoids allocating an object which is possibly not ever going to be used on startup of any application linking with the propgrid library (at the price of slower access to this object, but this should never be done in performance-critical parts of the code, hopefully) and avoids a problem with losing the previously registered editors when re-initializing wx modules, as wxPython does. Closes #23165. See #23187. --- include/wx/propgrid/propgrid.h | 9 ++++++++- src/propgrid/propgrid.cpp | 20 +++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/include/wx/propgrid/propgrid.h b/include/wx/propgrid/propgrid.h index e8300db5fd..59909651e6 100644 --- a/include/wx/propgrid/propgrid.h +++ b/include/wx/propgrid/propgrid.h @@ -113,7 +113,14 @@ public: int HasExtraStyle( int style ) const { return (m_extraStyle & style); } }; -extern WXDLLIMPEXP_DATA_PROPGRID(wxPGGlobalVarsClass*) wxPGGlobalVars; +// Internal class providing access to the global wxPGGlobalVars instance. +class WXDLLIMPEXP_PROPGRID wxPGGlobalVarsPtr +{ +public: + wxPGGlobalVarsClass* operator->() const; +}; + +extern WXDLLIMPEXP_DATA_PROPGRID(wxPGGlobalVarsPtr) wxPGGlobalVars; #define wxPGVariant_EmptyString (wxPGGlobalVars->m_vEmptyString) #define wxPGVariant_Zero (wxPGGlobalVars->m_vZero) diff --git a/src/propgrid/propgrid.cpp b/src/propgrid/propgrid.cpp index 13d00e749c..f55c9c2b9d 100644 --- a/src/propgrid/propgrid.cpp +++ b/src/propgrid/propgrid.cpp @@ -138,6 +138,9 @@ const char wxPropertyGridNameStr[] = "wxPropertyGrid"; // Statics in one class for easy destruction. // ----------------------------------------------------------------------- +// This is the real global wxPGGlobalVarsClass object allocated on demand. +static wxPGGlobalVarsClass* g_PGGlobalVars = nullptr; + #include "wx/module.h" class wxPGGlobalVarsClassManager : public wxModule @@ -145,8 +148,8 @@ class wxPGGlobalVarsClassManager : public wxModule wxDECLARE_DYNAMIC_CLASS(wxPGGlobalVarsClassManager); public: wxPGGlobalVarsClassManager() = default; - virtual bool OnInit() override { wxPGGlobalVars = new wxPGGlobalVarsClass(); return true; } - virtual void OnExit() override { wxDELETE(wxPGGlobalVars); } + virtual bool OnInit() override { return true; } + virtual void OnExit() override { wxDELETE(g_PGGlobalVars); } }; wxIMPLEMENT_DYNAMIC_CLASS(wxPGGlobalVarsClassManager, wxModule); @@ -161,8 +164,19 @@ void wxPGInitResourceModule() wxModule::InitializeModules(); } -wxPGGlobalVarsClass* wxPGGlobalVars = nullptr; +// This object is a proxy that allows us to only create wxPGGlobalVarsClass +// object when it's really needed. Note that we need to use an object for this +// instead of a function for compatibility reasons: a lot of existing code uses +// wxPGGlobalVars directly. +wxPGGlobalVarsPtr wxPGGlobalVars; +wxPGGlobalVarsClass* wxPGGlobalVarsPtr::operator->() const +{ + if ( !g_PGGlobalVars ) + g_PGGlobalVars = new wxPGGlobalVarsClass(); + + return g_PGGlobalVars; +} wxPGGlobalVarsClass::wxPGGlobalVarsClass() // Prepare some shared variants