From 2fbd56f4377102a0997b9683768bc17d1ab3c59a Mon Sep 17 00:00:00 2001 From: monojenkins Date: Mon, 20 Aug 2018 14:39:16 +0200 Subject: [PATCH] Fix process leak in process enumeration. Dispose rejected ones. (#10187) This https://github.com/mono/mono/issues/10143 and fix is from there. I considered nulling the array elements as I go, and then the tail, to help the GC, but does it help actually? I suspect not. --- mcs/class/System/System.Diagnostics/Process.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mcs/class/System/System.Diagnostics/Process.cs b/mcs/class/System/System.Diagnostics/Process.cs index 7e0800e581f3f..05690d654e4b4 100644 --- a/mcs/class/System/System.Diagnostics/Process.cs +++ b/mcs/class/System/System.Diagnostics/Process.cs @@ -508,9 +508,12 @@ public static Process[] GetProcessesByName(string processName, string machineNam int size = 0; for (int i = 0; i < processes.Length; i++) { + var process = processes[i]; try { - if (String.Compare (processName, processes[i].ProcessName, true) == 0) - processes [size++] = processes[i]; + if (String.Compare (processName, process.ProcessName, true) == 0) + processes [size++] = process; + else + process.Dispose(); } catch (SystemException) { /* The process might exit between GetProcesses_internal and GetProcessById */ }