From 05efbe376ed187895aa694b29bc755730e223e42 Mon Sep 17 00:00:00 2001 From: Teacup Date: Fri, 21 Jul 2023 19:32:13 -0400 Subject: [PATCH] fix: Stop HasProcessExited from leaking process handles (#603) Not closing process handles after using them. Every second. Smh. Imagine. Couldn't be me. --- WinApi.cs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/WinApi.cs b/WinApi.cs index fbfa967a..ab6768a6 100644 --- a/WinApi.cs +++ b/WinApi.cs @@ -28,6 +28,8 @@ namespace VRCX [DllImport("kernel32.dll", SetLastError = true)] public static extern bool GetExitCodeProcess(IntPtr hProcess, out uint lpExitCode); + [DllImport("kernel32.dll", SetLastError = true)] + public static extern bool CloseHandle(IntPtr hObject); /// /// Flag that specifies the access rights to query limited information about a process. @@ -54,13 +56,24 @@ namespace VRCX } bool exited; - if (!WinApi.GetExitCodeProcess(hProcess, out uint exitCode)) - { - throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()); - } - // Fun fact, If a program uses STILL_ACTIVE (259) as an exit code, GetExitCodeProcess will return 259, since it returns... the exit code. This would break this function. - exited = exitCode != 259; + try + { + if (!WinApi.GetExitCodeProcess(hProcess, out uint exitCode)) + { + throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()); + } + + // Fun fact, If a program uses STILL_ACTIVE (259) as an exit code, GetExitCodeProcess will return 259, since it returns... the exit code. This would break this function. + exited = exitCode != 259; + } + finally + { + // Imagine closing process handles. + WinApi.CloseHandle(hProcess); + } + + return exited; } }