mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 19:57:57 +01:00
Release ptr which get from CoCreateInstance to prevent memory leak (#39898)
<!-- Enter a brief description/summary of your PR here. What does it fix/what does it change/how was it tested (even manually, if necessary)? --> ## Summary of the Pull Request We need to release it manually. <!-- Please review the items on the PR checklist before submitting--> ## 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 <!-- Provide a more detailed description of the PR, other things fixed or any additional comments/features here --> ## Detailed Description of the Pull Request / Additional comments <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed --------- Co-authored-by: Yu Leng <yuleng@microsoft.com>
This commit is contained in:
@@ -53,6 +53,11 @@ internal static class DataSourceManager
|
|||||||
|
|
||||||
_dataSource.Initialize();
|
_dataSource.Initialize();
|
||||||
|
|
||||||
|
if (dataSourceObjPtr != IntPtr.Zero)
|
||||||
|
{
|
||||||
|
Marshal.Release(dataSourceObjPtr);
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,8 +30,9 @@ internal sealed partial class QueryStringBuilder
|
|||||||
if (queryHelper == null)
|
if (queryHelper == null)
|
||||||
{
|
{
|
||||||
ComWrappers cw = new StrategyBasedComWrappers();
|
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)
|
if (hr != 0)
|
||||||
{
|
{
|
||||||
throw new ArgumentException($"Failed to create SearchManager instance. HR: 0x{hr:X}");
|
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}");
|
throw new ArgumentException($"Failed to get catalog manager for {SystemIndex}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (searchManagerPtr != IntPtr.Zero)
|
||||||
|
{
|
||||||
|
Marshal.Release(searchManagerPtr);
|
||||||
|
}
|
||||||
|
|
||||||
queryHelper = catalogManager.GetQueryHelper();
|
queryHelper = catalogManager.GetQueryHelper();
|
||||||
if (queryHelper == null)
|
if (queryHelper == null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -71,10 +71,11 @@ public class VirtualDesktopHelper
|
|||||||
public VirtualDesktopHelper(bool desktopListUpdate = false)
|
public VirtualDesktopHelper(bool desktopListUpdate = false)
|
||||||
{
|
{
|
||||||
var cw = new StrategyBasedComWrappers();
|
var cw = new StrategyBasedComWrappers();
|
||||||
|
var virtualDesktopManagerPtr = IntPtr.Zero;
|
||||||
|
|
||||||
try
|
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)
|
if (hr != 0)
|
||||||
{
|
{
|
||||||
throw new ArgumentException($"Failed to create IVirtualDesktopManager instance. HR: 0x{hr:X}");
|
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 <VirtualDesktopHelper> failed: An exception was thrown when creating the instance of COM interface <IVirtualDesktopManager>. {ex} " });
|
ExtensionHost.LogMessage(new LogMessage() { Message = $"Initialization of <VirtualDesktopHelper> failed: An exception was thrown when creating the instance of COM interface <IVirtualDesktopManager>. {ex} " });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (virtualDesktopManagerPtr != IntPtr.Zero)
|
||||||
|
{
|
||||||
|
Marshal.Release(virtualDesktopManagerPtr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_isWindowsEleven = OSVersionHelper.IsWindows11();
|
_isWindowsEleven = OSVersionHelper.IsWindows11();
|
||||||
_desktopListAutoUpdate = desktopListUpdate;
|
_desktopListAutoUpdate = desktopListUpdate;
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
// See the LICENSE file in the project root for more information.
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Runtime.InteropServices.Marshalling;
|
using System.Runtime.InteropServices.Marshalling;
|
||||||
using ManagedCommon;
|
using ManagedCommon;
|
||||||
@@ -63,8 +64,9 @@ internal sealed partial class LaunchProfileAsAdminCommand : InvokableCommand
|
|||||||
private void Launch(string id, string profile)
|
private void Launch(string id, string profile)
|
||||||
{
|
{
|
||||||
ComWrappers cw = new StrategyBasedComWrappers();
|
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)
|
if (hr != 0)
|
||||||
{
|
{
|
||||||
@@ -95,6 +97,13 @@ internal sealed partial class LaunchProfileAsAdminCommand : InvokableCommand
|
|||||||
// _context.API.ShowMsg(name, message, string.Empty);
|
// _context.API.ShowMsg(name, message, string.Empty);
|
||||||
Logger.LogError($"Failed to open Windows Terminal: {ex.Message}");
|
Logger.LogError($"Failed to open Windows Terminal: {ex.Message}");
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (appManagerPtr != IntPtr.Zero)
|
||||||
|
{
|
||||||
|
Marshal.Release(appManagerPtr);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#pragma warning restore IDE0059, CS0168
|
#pragma warning restore IDE0059, CS0168
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
// See the LICENSE file in the project root for more information.
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Runtime.InteropServices.Marshalling;
|
using System.Runtime.InteropServices.Marshalling;
|
||||||
using ManagedCommon;
|
using ManagedCommon;
|
||||||
@@ -33,8 +34,9 @@ internal sealed partial class LaunchProfileCommand : InvokableCommand
|
|||||||
private void Launch(string id, string profile)
|
private void Launch(string id, string profile)
|
||||||
{
|
{
|
||||||
ComWrappers cw = new StrategyBasedComWrappers();
|
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)
|
if (hr != 0)
|
||||||
{
|
{
|
||||||
throw new ArgumentException($"Failed to create IApplicationActivationManager instance. HR: 0x{hr:X}");
|
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);
|
// _context.API.ShowMsg(name, message, string.Empty);
|
||||||
Logger.LogError($"Failed to open Windows Terminal: {ex.Message}");
|
Logger.LogError($"Failed to open Windows Terminal: {ex.Message}");
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (appManagerPtr != IntPtr.Zero)
|
||||||
|
{
|
||||||
|
Marshal.Release(appManagerPtr);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#pragma warning restore IDE0059, CS0168
|
#pragma warning restore IDE0059, CS0168
|
||||||
|
|
||||||
|
|||||||
@@ -23,10 +23,6 @@ public sealed partial class NativeHelpers
|
|||||||
ref Guid riid,
|
ref Guid riid,
|
||||||
out IntPtr rReturnedComObject);
|
out IntPtr rReturnedComObject);
|
||||||
|
|
||||||
#pragma warning disable CA2211 // Non-constant fields should not be visible
|
public static readonly Guid ApplicationActivationManagerCLSID = new Guid("45BA127D-10A8-46EA-8AB7-56EA9078943C");
|
||||||
#pragma warning disable SA1401 // Fields should be private
|
public static readonly Guid ApplicationActivationManagerIID = new Guid("2e941141-7f97-4756-ba1d-9decde894a3d");
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user