From 5e29b26d9e2f9364a1bfbe2f47eee3cc5396e72e Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 16 Oct 2015 20:20:42 +0200 Subject: [PATCH] Preserve Win32 last error in wxTlsKey::Get() ::TlsGetValue() resets the last error code which means that the previous last error is lost, but it shouldn't as we might be in the middle of logging it with wxLogSysError(). So preserve the last error explicitly. Closes #17209. --- include/wx/msw/tls.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/include/wx/msw/tls.h b/include/wx/msw/tls.h index 9d46cadf3e..e14fd541ca 100644 --- a/include/wx/msw/tls.h +++ b/include/wx/msw/tls.h @@ -34,7 +34,14 @@ public: // get the key value, there is no error return void *Get() const { - return ::TlsGetValue(m_slot); + // Exceptionally, TlsGetValue() calls SetLastError() even on success + // which means it overwrites the previous value. This is undesirable + // here, so explicitly preserve the last error here. + const DWORD dwLastError = ::GetLastError(); + void* const value = ::TlsGetValue(m_slot); + if ( dwLastError ) + ::SetLastError(dwLastError); + return value; } // change the key value, return true if ok