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.
This commit is contained in:
Vadim Zeitlin
2017-05-26 00:19:53 +02:00
parent b573c6a79f
commit 3543ae0177

View File

@@ -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<wxAnyToVariantRegistration*> m_anyToVariantRegs;
};
static wxAnyValueTypeGlobals* g_wxAnyValueTypeGlobals = NULL;
static wxScopedPtr<wxAnyValueTypeGlobals> 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:
};