Avoid spurious memory leak reports from static MSVC CRT

The leak check is done before the FlsAlloc()-installed callback function
runs in this build variant and so reports wxThreadSpecificInfo object,
and everything it contains, as leaked, even if it isn't, really, so
deallocate it explicitly ourselves in this case.

Also call wxThreadSpecificInfoTLS::CleanUp() from the main thread as
well, which fixes a possible real (albeit one-time only, so not really)
memory leak under XP where FLS API is not available.
This commit is contained in:
Vadim Zeitlin 2023-10-16 00:16:57 +02:00
parent 3f7660f6c1
commit cf66599eba
2 changed files with 9 additions and 0 deletions

View File

@ -238,6 +238,7 @@ Changes in behaviour which may result in build errors
wxMSW:
- Fix MSVS warning about redundant "const" in wx/itemid.h (#23590).
- Fix spurious memory leak reports when using static CRT.
3.2.3: (released 2023-10-10)

View File

@ -201,7 +201,12 @@ public:
static void CleanUp()
{
// To avoid bogus memory leaks reports when using debug version of
// static MSVC CRT we need to free memory ourselves even when it would
// have been done by FlsAlloc() callback because it does it too late.
#if !defined(_MSC_VER) || !defined(_DEBUG) || defined(_DLL)
if (!Instance().AllocCallback)
#endif
{
// FLS API was not available, which means that objects will not be freed automatically.
delete Get();
@ -1465,6 +1470,9 @@ bool wxThreadModule::OnInit()
void wxThreadModule::OnExit()
{
// Delete thread-specific info object for the main thread too, if any.
wxThreadSpecificInfoTLS::CleanUp();
if ( !::TlsFree(gs_tlsThisThread) )
{
wxLogLastError(wxT("TlsFree failed."));