From f7f874f77a6330771071d3f79126ae8661c52bcd Mon Sep 17 00:00:00 2001 From: Kenneth Porter Date: Thu, 15 Apr 2021 06:45:58 -0700 Subject: [PATCH] Invalidate TLS slot index when shutting down the library Not doing it could result in using the now invalid or, worse, reassigned to some other DLL, TLS slot if the library is initialized again later. Also add asserts checking that the index is initialized before using it. --- src/msw/thread.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/msw/thread.cpp b/src/msw/thread.cpp index 86c1ec6411..d864ba808a 100644 --- a/src/msw/thread.cpp +++ b/src/msw/thread.cpp @@ -526,6 +526,9 @@ THREAD_RETVAL wxThreadInternal::DoThreadStart(wxThread *thread) wxTRY { // store the thread object in the TLS + wxASSERT_MSG( gs_tlsThisThread != TLS_OUT_OF_INDEXES, + "TLS index not set. Is wx initialized?" ); + if ( !::TlsSetValue(gs_tlsThisThread, thread) ) { wxLogSysError(_("Cannot start thread: error writing TLS.")); @@ -914,6 +917,8 @@ bool wxThreadInternal::Resume() wxThread *wxThread::This() { + wxASSERT_MSG( gs_tlsThisThread != TLS_OUT_OF_INDEXES, + "TLS index not set. Is wx initialized?" ); wxThread *thread = (wxThread *)::TlsGetValue(gs_tlsThisThread); // be careful, 0 may be a valid return value as well @@ -1268,6 +1273,10 @@ void wxThreadModule::OnExit() wxLogLastError(wxT("TlsFree failed.")); } + // invalidate slot index to prevent wxThread from trying to reuse it if the + // library is initialized again later + gs_tlsThisThread = TLS_OUT_OF_INDEXES; + wxDELETE(gs_critsectThreadDelete); if ( gs_critsectGui )