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