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.
This commit is contained in:
Kenneth Porter
2021-04-15 06:45:58 -07:00
committed by Vadim Zeitlin
parent 5b0b50f164
commit f7f874f77a

View File

@@ -526,6 +526,9 @@ THREAD_RETVAL wxThreadInternal::DoThreadStart(wxThread *thread)
wxTRY wxTRY
{ {
// store the thread object in the TLS // 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) ) if ( !::TlsSetValue(gs_tlsThisThread, thread) )
{ {
wxLogSysError(_("Cannot start thread: error writing TLS.")); wxLogSysError(_("Cannot start thread: error writing TLS."));
@@ -914,6 +917,8 @@ bool wxThreadInternal::Resume()
wxThread *wxThread::This() wxThread *wxThread::This()
{ {
wxASSERT_MSG( gs_tlsThisThread != TLS_OUT_OF_INDEXES,
"TLS index not set. Is wx initialized?" );
wxThread *thread = (wxThread *)::TlsGetValue(gs_tlsThisThread); wxThread *thread = (wxThread *)::TlsGetValue(gs_tlsThisThread);
// be careful, 0 may be a valid return value as well // be careful, 0 may be a valid return value as well
@@ -1268,6 +1273,10 @@ void wxThreadModule::OnExit()
wxLogLastError(wxT("TlsFree failed.")); 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); wxDELETE(gs_critsectThreadDelete);
if ( gs_critsectGui ) if ( gs_critsectGui )