From 3543ae0177530c11ffa06c9a293164da8b8772a9 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 26 May 2017 00:19:53 +0200 Subject: [PATCH] Avoid memory leaks when wxWidgets is not used by the application It can happen that an application using wxWidgets doesn't actually use it, e.g. because it runs in the console mode and doesn't need the GUI stuff. In this case, g_wxAnyValueTypeGlobals was leaked because the cleanup function of the module, which is supposed to clean it up, was never executed. Fix this by making this pointer a smart pointer, thus making sure that the CRT cleans it up even if we don't. We still do it from the module OnExit() to account for the possibility of initializing and shutting down the library more than once. --- src/common/any.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/common/any.cpp b/src/common/any.cpp index 8cb31713b3..76f32a9824 100644 --- a/src/common/any.cpp +++ b/src/common/any.cpp @@ -28,6 +28,7 @@ #include "wx/module.h" #include "wx/hashmap.h" #include "wx/hashset.h" +#include "wx/scopedptr.h" using namespace wxPrivate; @@ -120,7 +121,7 @@ private: wxVector m_anyToVariantRegs; }; -static wxAnyValueTypeGlobals* g_wxAnyValueTypeGlobals = NULL; +static wxScopedPtr g_wxAnyValueTypeGlobals; WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImplVariantData) @@ -128,7 +129,7 @@ WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImplVariantData) void wxPreRegisterAnyToVariant(wxAnyToVariantRegistration* reg) { if ( !g_wxAnyValueTypeGlobals ) - g_wxAnyValueTypeGlobals = new wxAnyValueTypeGlobals(); + g_wxAnyValueTypeGlobals.reset(new wxAnyValueTypeGlobals()); g_wxAnyValueTypeGlobals->PreRegisterAnyToVariant(reg); } @@ -222,7 +223,7 @@ public: } virtual void OnExit() wxOVERRIDE { - wxDELETE(g_wxAnyValueTypeGlobals); + g_wxAnyValueTypeGlobals.reset(); } private: };