From df8ace3ab64c84a460af5fe6f5a773f201c582fe Mon Sep 17 00:00:00 2001 From: Yu Leng <42196638+moooyo@users.noreply.github.com> Date: Thu, 5 Jun 2025 15:02:03 +0800 Subject: [PATCH] Release ptr which get from CoCreateInstance to prevent memory leak (#39898) ## Summary of the Pull Request We need to release it manually. ## PR Checklist - [ ] **Closes:** #xxx - [ ] **Communication:** I've discussed this with core contributors already. If work hasn't been agreed, this work might be rejected - [ ] **Tests:** Added/updated and all pass - [ ] **Localization:** All end user facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx ## Detailed Description of the Pull Request / Additional comments ## Validation Steps Performed --------- Co-authored-by: Yu Leng --- .../Indexer/DataSourceManager.cs | 5 +++++ .../Indexer/Utils/QueryStringBuilder.cs | 8 +++++++- .../Helpers/VirtualDesktopHelper.cs | 10 +++++++++- .../Commands/LaunchProfileAsAdminCommand.cs | 11 ++++++++++- .../Commands/LaunchProfileCommand.cs | 11 ++++++++++- .../Helpers/NativeHelpers.cs | 8 ++------ 6 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/Indexer/DataSourceManager.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/Indexer/DataSourceManager.cs index 01c6d84357..5ddaf7b02d 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/Indexer/DataSourceManager.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/Indexer/DataSourceManager.cs @@ -53,6 +53,11 @@ internal static class DataSourceManager _dataSource.Initialize(); + if (dataSourceObjPtr != IntPtr.Zero) + { + Marshal.Release(dataSourceObjPtr); + } + return true; } } diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/Indexer/Utils/QueryStringBuilder.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/Indexer/Utils/QueryStringBuilder.cs index e025492de6..1e93fe7f80 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/Indexer/Utils/QueryStringBuilder.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/Indexer/Utils/QueryStringBuilder.cs @@ -30,8 +30,9 @@ internal sealed partial class QueryStringBuilder if (queryHelper == null) { ComWrappers cw = new StrategyBasedComWrappers(); + var searchManagerPtr = IntPtr.Zero; - var hr = NativeMethods.CoCreateInstance(ref Unsafe.AsRef(in NativeHelpers.CsWin32GUID.CLSIDSearchManager), IntPtr.Zero, NativeHelpers.CLSCTXINPROCALL, ref Unsafe.AsRef(in NativeHelpers.CsWin32GUID.IIDISearchManager), out var searchManagerPtr); + var hr = NativeMethods.CoCreateInstance(ref Unsafe.AsRef(in NativeHelpers.CsWin32GUID.CLSIDSearchManager), IntPtr.Zero, NativeHelpers.CLSCTXINPROCALL, ref Unsafe.AsRef(in NativeHelpers.CsWin32GUID.IIDISearchManager), out searchManagerPtr); if (hr != 0) { throw new ArgumentException($"Failed to create SearchManager instance. HR: 0x{hr:X}"); @@ -51,6 +52,11 @@ internal sealed partial class QueryStringBuilder throw new ArgumentException($"Failed to get catalog manager for {SystemIndex}"); } + if (searchManagerPtr != IntPtr.Zero) + { + Marshal.Release(searchManagerPtr); + } + queryHelper = catalogManager.GetQueryHelper(); if (queryHelper == null) { diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowWalker/Helpers/VirtualDesktopHelper.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowWalker/Helpers/VirtualDesktopHelper.cs index 112e2361de..b035040bde 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowWalker/Helpers/VirtualDesktopHelper.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowWalker/Helpers/VirtualDesktopHelper.cs @@ -71,10 +71,11 @@ public class VirtualDesktopHelper public VirtualDesktopHelper(bool desktopListUpdate = false) { var cw = new StrategyBasedComWrappers(); + var virtualDesktopManagerPtr = IntPtr.Zero; try { - var hr = NativeMethods.CoCreateInstance(ref this.iVirtualDesktopManagerCLSID, nint.Zero, CLSCTXINPROCALL, ref iVirtualDesktopManagerIID, out var virtualDesktopManagerPtr); + var hr = NativeMethods.CoCreateInstance(ref this.iVirtualDesktopManagerCLSID, nint.Zero, CLSCTXINPROCALL, ref iVirtualDesktopManagerIID, out virtualDesktopManagerPtr); if (hr != 0) { throw new ArgumentException($"Failed to create IVirtualDesktopManager instance. HR: 0x{hr:X}"); @@ -87,6 +88,13 @@ public class VirtualDesktopHelper ExtensionHost.LogMessage(new LogMessage() { Message = $"Initialization of failed: An exception was thrown when creating the instance of COM interface . {ex} " }); return; } + finally + { + if (virtualDesktopManagerPtr != IntPtr.Zero) + { + Marshal.Release(virtualDesktopManagerPtr); + } + } _isWindowsEleven = OSVersionHelper.IsWindows11(); _desktopListAutoUpdate = desktopListUpdate; diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowsTerminal/Commands/LaunchProfileAsAdminCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowsTerminal/Commands/LaunchProfileAsAdminCommand.cs index 4a5170250a..53c8e59cd4 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowsTerminal/Commands/LaunchProfileAsAdminCommand.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowsTerminal/Commands/LaunchProfileAsAdminCommand.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.InteropServices.Marshalling; using ManagedCommon; @@ -63,8 +64,9 @@ internal sealed partial class LaunchProfileAsAdminCommand : InvokableCommand private void Launch(string id, string profile) { ComWrappers cw = new StrategyBasedComWrappers(); + var appManagerPtr = IntPtr.Zero; - var hr = NativeHelpers.CoCreateInstance(ref NativeHelpers.ApplicationActivationManagerCLSID, IntPtr.Zero, NativeHelpers.CLSCTXINPROCALL, ref NativeHelpers.ApplicationActivationManagerIID, out var appManagerPtr); + var hr = NativeHelpers.CoCreateInstance(ref Unsafe.AsRef(in NativeHelpers.ApplicationActivationManagerCLSID), IntPtr.Zero, NativeHelpers.CLSCTXINPROCALL, ref Unsafe.AsRef(in NativeHelpers.ApplicationActivationManagerIID), out appManagerPtr); if (hr != 0) { @@ -95,6 +97,13 @@ internal sealed partial class LaunchProfileAsAdminCommand : InvokableCommand // _context.API.ShowMsg(name, message, string.Empty); Logger.LogError($"Failed to open Windows Terminal: {ex.Message}"); } + finally + { + if (appManagerPtr != IntPtr.Zero) + { + Marshal.Release(appManagerPtr); + } + } } #pragma warning restore IDE0059, CS0168 diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowsTerminal/Commands/LaunchProfileCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowsTerminal/Commands/LaunchProfileCommand.cs index 5f9d4ea00d..c7a1dec014 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowsTerminal/Commands/LaunchProfileCommand.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowsTerminal/Commands/LaunchProfileCommand.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.InteropServices.Marshalling; using ManagedCommon; @@ -33,8 +34,9 @@ internal sealed partial class LaunchProfileCommand : InvokableCommand private void Launch(string id, string profile) { ComWrappers cw = new StrategyBasedComWrappers(); + var appManagerPtr = IntPtr.Zero; - var hr = NativeHelpers.CoCreateInstance(ref NativeHelpers.ApplicationActivationManagerCLSID, IntPtr.Zero, NativeHelpers.CLSCTXINPROCALL, ref NativeHelpers.ApplicationActivationManagerIID, out var appManagerPtr); + var hr = NativeHelpers.CoCreateInstance(ref Unsafe.AsRef(in NativeHelpers.ApplicationActivationManagerCLSID), IntPtr.Zero, NativeHelpers.CLSCTXINPROCALL, ref Unsafe.AsRef(in NativeHelpers.ApplicationActivationManagerIID), out appManagerPtr); if (hr != 0) { throw new ArgumentException($"Failed to create IApplicationActivationManager instance. HR: 0x{hr:X}"); @@ -64,6 +66,13 @@ internal sealed partial class LaunchProfileCommand : InvokableCommand // _context.API.ShowMsg(name, message, string.Empty); Logger.LogError($"Failed to open Windows Terminal: {ex.Message}"); } + finally + { + if (appManagerPtr != IntPtr.Zero) + { + Marshal.Release(appManagerPtr); + } + } } #pragma warning restore IDE0059, CS0168 diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowsTerminal/Helpers/NativeHelpers.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowsTerminal/Helpers/NativeHelpers.cs index e431d15a1b..254a8c3dcf 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowsTerminal/Helpers/NativeHelpers.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowsTerminal/Helpers/NativeHelpers.cs @@ -23,10 +23,6 @@ public sealed partial class NativeHelpers ref Guid riid, out IntPtr rReturnedComObject); -#pragma warning disable CA2211 // Non-constant fields should not be visible -#pragma warning disable SA1401 // Fields should be private - public static Guid ApplicationActivationManagerCLSID = new Guid("45BA127D-10A8-46EA-8AB7-56EA9078943C"); - public static Guid ApplicationActivationManagerIID = new Guid("2e941141-7f97-4756-ba1d-9decde894a3d"); -#pragma warning restore SA1401 // Fields should be private -#pragma warning restore CA2211 // Non-constant fields should not be visible + public static readonly Guid ApplicationActivationManagerCLSID = new Guid("45BA127D-10A8-46EA-8AB7-56EA9078943C"); + public static readonly Guid ApplicationActivationManagerIID = new Guid("2e941141-7f97-4756-ba1d-9decde894a3d"); }