From 2321ce27b66003d2903179e70b7cb60329d2b63d Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Mon, 31 Jul 2023 15:08:49 -0700 Subject: [PATCH] Don't leak a reference to the COM object returned from ADsOpenObject. --- .../DirectoryServices/AccountManagement/interopt.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.DirectoryServices.AccountManagement/src/System/DirectoryServices/AccountManagement/interopt.cs b/src/libraries/System.DirectoryServices.AccountManagement/src/System/DirectoryServices/AccountManagement/interopt.cs index 5ff280398e7884..fe164bc8a926e2 100644 --- a/src/libraries/System.DirectoryServices.AccountManagement/src/System/DirectoryServices/AccountManagement/interopt.cs +++ b/src/libraries/System.DirectoryServices.AccountManagement/src/System/DirectoryServices/AccountManagement/interopt.cs @@ -19,9 +19,10 @@ internal static class UnsafeNativeMethods { public static int ADsOpenObject(string path, string userName, string password, int flags, [In, Out] ref Guid iid, [Out, MarshalAs(UnmanagedType.Interface)] out object ppObject) { + IntPtr ppObjPtr = IntPtr.Zero; try { - int hr = Interop.Activeds.ADsOpenObject(path, userName, password, flags, ref iid, out IntPtr ppObjPtr); + int hr = Interop.Activeds.ADsOpenObject(path, userName, password, flags, ref iid, out ppObjPtr); ppObject = Marshal.GetObjectForIUnknown(ppObjPtr); return hr; } @@ -29,6 +30,13 @@ public static int ADsOpenObject(string path, string userName, string password, i { throw new InvalidOperationException(SR.AdsiNotInstalled); } + finally + { + if (ppObjPtr != IntPtr.Zero) + { + Marshal.Release(ppObjPtr); + } + } } //