Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement: make vld preserve GetLastError/WSAGetLastError state #25

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/stdafx.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#pragma once

#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")

#include <cassert>
#include <cerrno>
#include <cstdio>
Expand Down
19 changes: 19 additions & 0 deletions src/vld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
#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.
extern vldblockheader_t *g_vldBlockList;
extern HANDLE g_vldHeap;
Expand Down Expand Up @@ -1293,10 +1296,26 @@ 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
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

if (tls == NULL) {
DWORD threadId = GetCurrentThreadId();

Expand Down