From 2b8c96fe35a35627efb2cb108e050854bd8eca23 Mon Sep 17 00:00:00 2001 From: Philip Rebohle Date: Sun, 22 Jul 2018 02:24:04 +0200 Subject: [PATCH] [util] Fix reference counting for thread objects Fixes a potential race when threads run out of scope before the thread function has started. --- src/util/thread.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/util/thread.h b/src/util/thread.h index c779895c2..46cb3213b 100644 --- a/src/util/thread.h +++ b/src/util/thread.h @@ -26,6 +26,9 @@ namespace dxvk { ThreadFn(Proc&& proc) : m_proc(std::move(proc)) { + // Reference for the thread function + this->incRef(); + m_handle = ::CreateThread(nullptr, 0, ThreadFn::threadProc, this, 0, nullptr); @@ -59,8 +62,9 @@ namespace dxvk { HANDLE m_handle; static DWORD WINAPI threadProc(void *arg) { - Rc info = reinterpret_cast(arg); - info->m_proc(); + auto thread = reinterpret_cast(arg); + thread->m_proc(); + thread->decRef(); return 0; }