From cf66599eba39810d40da01a03d0ec87a4b03a3d2 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 16 Oct 2023 00:16:57 +0200 Subject: [PATCH] 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. --- docs/changes.txt | 1 + src/msw/thread.cpp | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/docs/changes.txt b/docs/changes.txt index 281bdf244d..6eae8eaf00 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -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) diff --git a/src/msw/thread.cpp b/src/msw/thread.cpp index 3bd1075279..31ac8b3d58 100644 --- a/src/msw/thread.cpp +++ b/src/msw/thread.cpp @@ -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."));