aboutsummaryrefslogtreecommitdiff
path: root/src/sync.cpp
diff options
context:
space:
mode:
authorHennadii Stepanov <32963518+hebasto@users.noreply.github.com>2020-05-05 08:07:47 +0300
committerHennadii Stepanov <32963518+hebasto@users.noreply.github.com>2020-05-19 01:13:49 +0300
commit458992b06d80eb568141f60a33d38e12e894e27a (patch)
treee4ceacb8391cccd46765ee41f4f75dc968afc7cc /src/sync.cpp
parentec79b5f86b22ad8f77c736f9bb76c2e4d7faeaa4 (diff)
downloadbitcoin-458992b06d80eb568141f60a33d38e12e894e27a.tar.xz
Prevent UB in DeleteLock() function
Diffstat (limited to 'src/sync.cpp')
-rw-r--r--src/sync.cpp19
1 files changed, 5 insertions, 14 deletions
diff --git a/src/sync.cpp b/src/sync.cpp
index b86c57e498..afb5ad51b1 100644
--- a/src/sync.cpp
+++ b/src/sync.cpp
@@ -78,21 +78,16 @@ typedef std::map<std::pair<void*, void*>, LockStack> LockOrders;
typedef std::set<std::pair<void*, void*> > InvLockOrders;
struct LockData {
- // Very ugly hack: as the global constructs and destructors run single
- // threaded, we use this boolean to know whether LockData still exists,
- // as DeleteLock can get called by global RecursiveMutex destructors
- // after LockData disappears.
- bool available;
- LockData() : available(true) {}
- ~LockData() { available = false; }
-
LockOrders lockorders;
InvLockOrders invlockorders;
std::mutex dd_mutex;
};
+
LockData& GetLockData() {
- static LockData lockdata;
- return lockdata;
+ // This approach guarantees that the object is not destroyed until after its last use.
+ // The operating system automatically reclaims all the memory in a program's heap when that program exits.
+ static LockData& lock_data = *new LockData();
+ return lock_data;
}
static thread_local LockStack g_lockstack;
@@ -207,10 +202,6 @@ void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLi
void DeleteLock(void* cs)
{
LockData& lockdata = GetLockData();
- if (!lockdata.available) {
- // We're already shutting down.
- return;
- }
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
std::pair<void*, void*> item = std::make_pair(cs, nullptr);
LockOrders::iterator it = lockdata.lockorders.lower_bound(item);