Skip to content
Open
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
7 changes: 5 additions & 2 deletions include/wil/registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -3123,14 +3123,17 @@ namespace details
if (0 == ::InterlockedDecrement(&m_refCount))
{
lock.reset(); // leave the lock before deleting it.
// Make sure callbacks are not running in parallel to destruction.
m_threadPoolWait.reset();
delete this;
}
}

void ReleaseFromCallback(bool rearm)
{
auto lock = m_lock.lock_exclusive();
if (0 == ::InterlockedDecrement(&m_refCount))
auto refCount = ::InterlockedDecrement(&m_refCount);
if (0 == refCount)
{
// Destroy the thread pool wait now to avoid the wait that would occur in the
// destructor. That wait would cause a deadlock since we are doing this from the callback.
Expand All @@ -3139,7 +3142,7 @@ namespace details
delete this;
// Sleep(1); // Enable for testing to find use after free bugs.
}
else if (rearm)
else if (rearm && refCount > 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This suggests you are guarding against negative values. If you've seen this actually happen, we'd be very interested to learn more since that would suggest there's a mis-match of AddRef and Release.

{
::SetThreadpoolWait(m_threadPoolWait.get(), m_eventHandle.get(), nullptr);
}
Expand Down