Add missing Free methods for our Com interface marshallers. (#87777)
authorJeremy Koritzinsky <jekoritz@microsoft.com>
Tue, 20 Jun 2023 03:18:18 +0000 (20:18 -0700)
committerGitHub <noreply@github.com>
Tue, 20 Jun 2023 03:18:18 +0000 (20:18 -0700)
src/libraries/System.Runtime.InteropServices/ref/System.Runtime.InteropServices.cs
src/libraries/System.Runtime.InteropServices/src/System/Runtime/InteropServices/Marshalling/ComInterfaceMarshaller.cs
src/libraries/System.Runtime.InteropServices/src/System/Runtime/InteropServices/Marshalling/FreeThreadedStrategy.cs
src/libraries/System.Runtime.InteropServices/src/System/Runtime/InteropServices/Marshalling/UniqueComInterfaceMarshaller.cs

index 87d4310..24fb2b3 100644 (file)
@@ -342,6 +342,7 @@ namespace System.Runtime.InteropServices.Marshalling
     {
         public static void* ConvertToUnmanaged(T? managed) { throw null; }
         public static T? ConvertToManaged(void* unmanaged) { throw null; }
+        public static void Free(void* unmanaged) { }
     }
     public sealed partial class ComObject : System.Runtime.InteropServices.IDynamicInterfaceCastable, System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider
     {
@@ -474,6 +475,7 @@ namespace System.Runtime.InteropServices.Marshalling
     {
         public static void* ConvertToUnmanaged(T? managed) { throw null; }
         public static T? ConvertToManaged(void* unmanaged) { throw null; }
+        public static void Free(void* unmanaged) { }
     }
     [System.CLSCompliantAttribute(false)]
     public readonly partial struct VirtualMethodTableInfo
index 382540a..ee05af5 100644 (file)
@@ -44,6 +44,14 @@ namespace System.Runtime.InteropServices.Marshalling
             return (T)StrategyBasedComWrappers.DefaultMarshallingInstance.GetOrCreateObjectForComInstance((nint)unmanaged, CreateObjectFlags.Unwrap);
         }
 
+        public static void Free(void* unmanaged)
+        {
+            if (unmanaged != null)
+            {
+                Marshal.Release((nint)unmanaged);
+            }
+        }
+
         internal static void* CastIUnknownToInterfaceType(nint unknown)
         {
             if (TargetInterfaceIID is null)
index e318b42..054bf57 100644 (file)
@@ -12,6 +12,7 @@ namespace System.Runtime.InteropServices.Marshalling
 
         void* IIUnknownStrategy.CreateInstancePointer(void* unknown)
         {
+            Marshal.AddRef((nint)unknown);
             return unknown;
         }
 
index 1fdc5ea..2e48b89 100644 (file)
@@ -42,5 +42,13 @@ namespace System.Runtime.InteropServices.Marshalling
             }
             return (T)StrategyBasedComWrappers.DefaultMarshallingInstance.GetOrCreateObjectForComInstance((nint)unmanaged, CreateObjectFlags.Unwrap | CreateObjectFlags.UniqueInstance);
         }
+
+        public static void Free(void* unmanaged)
+        {
+            if (unmanaged != null)
+            {
+                Marshal.Release((nint)unmanaged);
+            }
+        }
     }
 }