From f8b3ecbb0ec76ae56f0db9da4d463cfa608c9df4 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 30 May 2017 19:18:53 +0200 Subject: [PATCH] Fix bug due to undefined g_wxAnyValueTypeGlobals initialization order This fixes the changes of commit 3543ae0177530c11ffa06c9a293164da8b8772a9 which didn't work if any of the globals using wxPreRegisterAnyToVariant() were instantiated before g_wxAnyValueTypeGlobals itself. Wrap the global inside a function to ensure that we initialized it before it is used by wxPreRegisterAnyToVariant() and not after. --- src/common/any.cpp | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/common/any.cpp b/src/common/any.cpp index 76f32a9824..b4141c47e5 100644 --- a/src/common/any.cpp +++ b/src/common/any.cpp @@ -121,16 +121,27 @@ private: wxVector m_anyToVariantRegs; }; -static wxScopedPtr g_wxAnyValueTypeGlobals; +static wxScopedPtr& GetAnyValueTypeGlobals() +{ + static wxScopedPtr s_wxAnyValueTypeGlobals; + if ( !s_wxAnyValueTypeGlobals ) + { + // Notice that it is _not_ sufficient to just initialize the static + // object like this because it can be used after it was reset by + // wxAnyValueTypeGlobalsManager if the library is shut down and then + // initialized again. + s_wxAnyValueTypeGlobals.reset(new wxAnyValueTypeGlobals()); + } + + return s_wxAnyValueTypeGlobals; +} WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImplVariantData) void wxPreRegisterAnyToVariant(wxAnyToVariantRegistration* reg) { - if ( !g_wxAnyValueTypeGlobals ) - g_wxAnyValueTypeGlobals.reset(new wxAnyValueTypeGlobals()); - g_wxAnyValueTypeGlobals->PreRegisterAnyToVariant(reg); + GetAnyValueTypeGlobals()->PreRegisterAnyToVariant(reg); } bool wxConvertAnyToVariant(const wxAny& any, wxVariant* variant) @@ -175,7 +186,7 @@ bool wxConvertAnyToVariant(const wxAny& any, wxVariant* variant) // Find matching factory function wxVariantDataFactory f = - g_wxAnyValueTypeGlobals->FindVariantDataFactory(any.GetType()); + GetAnyValueTypeGlobals()->FindVariantDataFactory(any.GetType()); wxVariantData* data = NULL; @@ -223,7 +234,7 @@ public: } virtual void OnExit() wxOVERRIDE { - g_wxAnyValueTypeGlobals.reset(); + GetAnyValueTypeGlobals().reset(); } private: };