From 700960cc96dfb343b5e7d71006634f212673231a Mon Sep 17 00:00:00 2001 From: rglarix Date: Mon, 20 Feb 2017 12:52:08 +0100 Subject: [PATCH 1/2] enhancement: preserve WSASetLastError value Signed-off-by: rglarix --- src/stdafx.h | 4 ++++ src/vld.cpp | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/stdafx.h b/src/stdafx.h index a968c068..3dba52fc 100644 --- a/src/stdafx.h +++ b/src/stdafx.h @@ -1,5 +1,9 @@ #pragma once +#include +#include +#pragma comment(lib, "ws2_32.lib") + #include #include #include diff --git a/src/vld.cpp b/src/vld.cpp index d19295ff..182ade2b 100644 --- a/src/vld.cpp +++ b/src/vld.cpp @@ -42,6 +42,8 @@ #define HEAP_MAP_RESERVE 2 // Usually there won't be more than a few heaps in the process, so this should be small. #define MODULE_SET_RESERVE 16 // There are likely to be several modules loaded in the process. +#define PRESERVE_WSAERROR // if defined preserves status of WSAGetLastError + // Imported global variables. extern vldblockheader_t *g_vldBlockList; extern HANDLE g_vldHeap; @@ -1293,10 +1295,20 @@ SIZE_T VisualLeakDetector::eraseDuplicates (const BlockMap::Iterator &element, S // tls_t* VisualLeakDetector::getTls () { +#if defined(PRESERVE_WSAERROR) + // save winsock last error because TlsGetValue resets it + // perhaps we should do the same wit lasterror ? + int wsaerrpre = WSAGetLastError(); +#endif + // Get the pointer to this thread's thread local storage structure. tls_t* tls = (tls_t*)TlsGetValue(m_tlsIndex); assert(GetLastError() == ERROR_SUCCESS); +#if defined(PRESERVE_WSAERROR) + WSASetLastError(wsaerrpre); // restore previous state of ws error +#endif + if (tls == NULL) { DWORD threadId = GetCurrentThreadId(); From 44b5c5916008639ea4e60a061ef0c95f8d496dbd Mon Sep 17 00:00:00 2001 From: rglarix Date: Tue, 7 Mar 2017 17:06:27 +0100 Subject: [PATCH 2/2] enhancement: preserve GetLastError value Signed-off-by: rglarix --- src/vld.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/vld.cpp b/src/vld.cpp index 182ade2b..12df213e 100644 --- a/src/vld.cpp +++ b/src/vld.cpp @@ -42,6 +42,7 @@ #define HEAP_MAP_RESERVE 2 // Usually there won't be more than a few heaps in the process, so this should be small. #define MODULE_SET_RESERVE 16 // There are likely to be several modules loaded in the process. +#define PRESERVE_LASTERROR // if defined preserves status of GetLastError #define PRESERVE_WSAERROR // if defined preserves status of WSAGetLastError // Imported global variables. @@ -1297,14 +1298,20 @@ tls_t* VisualLeakDetector::getTls () { #if defined(PRESERVE_WSAERROR) // save winsock last error because TlsGetValue resets it - // perhaps we should do the same wit lasterror ? int wsaerrpre = WSAGetLastError(); #endif +#if defined(PRESERVE_LASTERROR) + // save last error because TlsGetValue resets it + int errpre = GetLastError(); +#endif // Get the pointer to this thread's thread local storage structure. tls_t* tls = (tls_t*)TlsGetValue(m_tlsIndex); assert(GetLastError() == ERROR_SUCCESS); +#if defined(PRESERVE_LASTERROR) + SetLastError(errpre); // restore previous state of error +#endif #if defined(PRESERVE_WSAERROR) WSASetLastError(wsaerrpre); // restore previous state of ws error #endif