From 3313dba563cb45af358b4a3278029994c40fbdb1 Mon Sep 17 00:00:00 2001 From: Jackson Schuster <36744439+jtschuster@users.noreply.github.com> Date: Fri, 12 Dec 2025 09:23:54 -0800 Subject: [PATCH 1/2] Add AddRef to callee that caches interface ptr --- .../NativeMarshallingAttributeTests.cs | 1 - .../NativeExports/ComInterfaceGenerator/UniqueMarshalling.cs | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/NativeMarshallingAttributeTests.cs b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/NativeMarshallingAttributeTests.cs index 22d71f56ea093e..b06b6c0709064e 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/NativeMarshallingAttributeTests.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/NativeMarshallingAttributeTests.cs @@ -17,7 +17,6 @@ partial class NativeExportsNE public class NativeMarshallingAttributeTests { [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsCoreCLR))] - [ActiveIssue("https://github.com/dotnet/runtime/issues/122447")] public void GetSameComInterfaceTwiceReturnsUniqueInstances() { // When using NativeMarshalling with UniqueComInterfaceMarshaller, diff --git a/src/libraries/System.Runtime.InteropServices/tests/TestAssets/NativeExports/ComInterfaceGenerator/UniqueMarshalling.cs b/src/libraries/System.Runtime.InteropServices/tests/TestAssets/NativeExports/ComInterfaceGenerator/UniqueMarshalling.cs index f7dc257e01261f..e1099e2ce41fb7 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/TestAssets/NativeExports/ComInterfaceGenerator/UniqueMarshalling.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/TestAssets/NativeExports/ComInterfaceGenerator/UniqueMarshalling.cs @@ -23,6 +23,8 @@ public static unsafe class UniqueMarshalling s_cachedPtr = (void*)ptr; } + // AddRef before returning - caller will Release + Marshal.AddRef((nint)s_cachedPtr); return s_cachedPtr; } } From 6fe03f872079494b71a7dbcc321c1593dec217e9 Mon Sep 17 00:00:00 2001 From: jtschuster <36744439+jtschuster@users.noreply.github.com> Date: Mon, 15 Dec 2025 10:43:43 -0800 Subject: [PATCH 2/2] Cache a ComObject in a static field for the last release --- .../NativeExports/ComInterfaceGenerator/UniqueMarshalling.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/libraries/System.Runtime.InteropServices/tests/TestAssets/NativeExports/ComInterfaceGenerator/UniqueMarshalling.cs b/src/libraries/System.Runtime.InteropServices/tests/TestAssets/NativeExports/ComInterfaceGenerator/UniqueMarshalling.cs index e1099e2ce41fb7..371a973076e97b 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/TestAssets/NativeExports/ComInterfaceGenerator/UniqueMarshalling.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/TestAssets/NativeExports/ComInterfaceGenerator/UniqueMarshalling.cs @@ -10,6 +10,8 @@ namespace NativeExports.ComInterfaceGenerator public static unsafe class UniqueMarshalling { private static void* s_cachedPtr = null; + // To handle the final release of the cached pointer + private static ComObject s_cachedObject = null; // Call from another assembly to get a ptr to make an RCW [UnmanagedCallersOnly(EntryPoint = "new_unique_marshalling")] @@ -20,6 +22,7 @@ public static unsafe class UniqueMarshalling StrategyBasedComWrappers wrappers = new(); var myObject = new SharedTypes.ComInterfaces.UniqueMarshalling(); nint ptr = wrappers.GetOrCreateComInterfaceForObject(myObject, CreateComInterfaceFlags.None); + s_cachedObject = (ComObject)wrappers.GetOrCreateObjectForComInstance(ptr, CreateObjectFlags.None); s_cachedPtr = (void*)ptr; }