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); + } + } } //