Skip to content

Commit 5a4aa73

Browse files
committed
Fix a reference-counting bug in ScopedSharedMutexReader.
When the copy constructor is called m_readers needs to be incremented because the destructor will be calkled on both the original object and the copy.
1 parent fd4a62c commit 5a4aa73

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

lldb/include/lldb/Core/SwiftScratchContextReader.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class SharedMutex {
4040
}
4141
bool unlock_shared() {
4242
std::lock_guard<std::mutex> lock(m_reader_mutex);
43+
assert(m_readers > 0);
4344
--m_readers;
4445
return m_impl.unlock_shared();
4546
}
@@ -57,7 +58,12 @@ struct ScopedSharedMutexReader {
5758
if (m_mutex)
5859
m_mutex->lock_shared();
5960
}
60-
ScopedSharedMutexReader(const ScopedSharedMutexReader&) = default;
61+
62+
ScopedSharedMutexReader(const ScopedSharedMutexReader &copy)
63+
: m_mutex(copy.m_mutex) {
64+
if (m_mutex)
65+
m_mutex->lock_shared();
66+
}
6167

6268
~ScopedSharedMutexReader() {
6369
if (m_mutex)
@@ -101,9 +107,6 @@ class SwiftScratchContextReader : ScopedSharedMutexReader {
101107
assert(m_ptr && "invalid context");
102108
}
103109

104-
SwiftScratchContextReader(const SwiftScratchContextReader &copy)
105-
: ScopedSharedMutexReader(copy.m_mutex), m_ptr(copy.m_ptr) {}
106-
107110
TypeSystemSwiftTypeRefForExpressions *get() {
108111
assert(m_ptr && "invalid context");
109112
return m_ptr;

0 commit comments

Comments
 (0)