From 7038aa83b393146ceaaa5ada74fdd76beef150ac Mon Sep 17 00:00:00 2001 From: Frediano Ziglio Date: Sun, 8 Jul 2018 20:48:56 +0100 Subject: [PATCH 41/43] vdagent: Stop correctly helper thread The thread launched to detect desktop switches events is not stopped correctly causing potentially dandling pointers. Queue a APC to make the loop exit and wait for thread termination from the main thread. Signed-off-by: Frediano Ziglio Acked-by: Christophe Fergeau --- vdagent/vdagent.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/vdagent/vdagent.cpp b/vdagent/vdagent.cpp index 306bfbd..e577679 100644 --- a/vdagent/vdagent.cpp +++ b/vdagent/vdagent.cpp @@ -238,20 +238,27 @@ DWORD WINAPI VDAgent::event_thread_proc(LPVOID param) return 1; } while (agent->_running) { - DWORD wait_ret = WaitForSingleObject(desktop_event, INFINITE); + DWORD wait_ret = WaitForSingleObjectEx(desktop_event, INFINITE, TRUE); switch (wait_ret) { case WAIT_OBJECT_0: agent->set_control_event(CONTROL_DESKTOP_SWITCH); break; + case WAIT_IO_COMPLETION: + // handle APC events + break; case WAIT_TIMEOUT: default: - vd_printf("WaitForSingleObject(): %lu", wait_ret); + vd_printf("WaitForSingleObjectEx(): %lu", wait_ret); } } CloseHandle(desktop_event); return 0; } +static VOID CALLBACK event_thread_stop_proc(ULONG_PTR) +{ +} + bool VDAgent::run() { DWORD session_id; @@ -333,8 +340,12 @@ bool VDAgent::run() set_clipboard_owner(owner_none); } } - vd_printf("Agent stopped"); + if (!QueueUserAPC(event_thread_stop_proc, event_thread, 0)) { + TerminateThread(event_thread, 0); + } + WaitForSingleObject(event_thread, INFINITE); CloseHandle(event_thread); + vd_printf("Agent stopped"); return true; } -- 2.17.1