Skip to content

Commit cf66599

Browse files
committed
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.
1 parent 3f7660f commit cf66599

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

docs/changes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ Changes in behaviour which may result in build errors
238238
wxMSW:
239239

240240
- Fix MSVS warning about redundant "const" in wx/itemid.h (#23590).
241+
- Fix spurious memory leak reports when using static CRT.
241242

242243

243244
3.2.3: (released 2023-10-10)

src/msw/thread.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,12 @@ class wxThreadSpecificInfoTLS
201201

202202
static void CleanUp()
203203
{
204+
// To avoid bogus memory leaks reports when using debug version of
205+
// static MSVC CRT we need to free memory ourselves even when it would
206+
// have been done by FlsAlloc() callback because it does it too late.
207+
#if !defined(_MSC_VER) || !defined(_DEBUG) || defined(_DLL)
204208
if (!Instance().AllocCallback)
209+
#endif
205210
{
206211
// FLS API was not available, which means that objects will not be freed automatically.
207212
delete Get();
@@ -1465,6 +1470,9 @@ bool wxThreadModule::OnInit()
14651470

14661471
void wxThreadModule::OnExit()
14671472
{
1473+
// Delete thread-specific info object for the main thread too, if any.
1474+
wxThreadSpecificInfoTLS::CleanUp();
1475+
14681476
if ( !::TlsFree(gs_tlsThisThread) )
14691477
{
14701478
wxLogLastError(wxT("TlsFree failed."));

0 commit comments

Comments
 (0)