From fa46914a394a7f6e972b4dc85b7ebaff2fb82074 Mon Sep 17 00:00:00 2001 From: Jackson Schuster <36744439+jtschuster@users.noreply.github.com> Date: Mon, 19 Jun 2023 10:13:45 -0700 Subject: [PATCH 1/6] add work --- ...mInterfaces.ICollectionMarshallingFails.cs | 9 +- ...Generator.Tests.ComInterfaces.IIntArray.cs | 13 +- ...tor.Tests.ComInterfaces.IJaggedIntArray.cs | 37 +- ...erfaces.IJaggedIntArrayMarshallingFails.cs | 37 +- ...nterfaces.ICustomStringMarshallingUtf16.cs | 4 +- .../SharedTypes.ComInterfaces.IDerived.cs | 4 +- .../SharedTypes.ComInterfaces.IGetIntArray.cs | 6 +- ...omInterfaces.IStringMarshallingOverride.cs | 12 +- ...faces.IStringMarshallingOverrideDerived.cs | 24 +- ...edTypes.ComInterfaces.IUTF16Marshalling.cs | 4 +- ...redTypes.ComInterfaces.IUTF8Marshalling.cs | 6 +- .../ManagedToNativeStubs.g.cs | 19 +- .../NativeToManagedStubs.g.cs | 13 + .../LibraryImports.g.cs | 4 +- .../LibraryImports.g.cs | 1346 +++++++++++++++-- 15 files changed, 1352 insertions(+), 186 deletions(-) diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.ICollectionMarshallingFails.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.ICollectionMarshallingFails.cs index e0ce879..30d2ac3 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.ICollectionMarshallingFails.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.ICollectionMarshallingFails.cs @@ -15,9 +15,9 @@ int[] global::ComInterfaceGenerator.Tests.ComInterfaces.ICollectionMarshallingFails.Get() { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::ComInterfaceGenerator.Tests.ComInterfaces.ICollectionMarshallingFails)); - int[] __retVal; + int[] __retVal = default; nint* __retVal_native = default; - int __invokeRetVal; + int __invokeRetVal = default; // Setup - Perform required setup. int __retVal_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__numElements); @@ -44,6 +44,7 @@ finally { // Cleanup - Perform required cleanup. + __retVal_native__numElements = 10; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); } @@ -56,7 +57,7 @@ { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::ComInterfaceGenerator.Tests.ComInterfaces.ICollectionMarshallingFails)); nint* __value_native = default; - int __invokeRetVal; + int __invokeRetVal = default; // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn __value_native__marshaller = new(); int __value_native__numElements; @@ -106,6 +107,7 @@ internal static int ABI_Get(System.Runtime.InteropServices.ComWrappers.ComInterf // Setup - Perform required setup. int __invokeRetValUnmanaged__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __invokeRetValUnmanaged__numElements); + ; try { // Unmarshal - Convert native data to managed data. @@ -140,6 +142,7 @@ internal static int ABI_Set(System.Runtime.InteropServices.ComWrappers.ComInterf // Setup - Perform required setup. int __value_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __value_native__numElements); + ; try { // Unmarshal - Convert native data to managed data. diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IIntArray.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IIntArray.cs index ca46b4a..5d4874d 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IIntArray.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IIntArray.cs @@ -16,9 +16,9 @@ { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::ComInterfaceGenerator.Tests.ComInterfaces.IIntArray)); System.Runtime.CompilerServices.Unsafe.SkipInit(out size); - int[] __retVal; + int[] __retVal = default; int* __retVal_native = default; - int __invokeRetVal; + int __invokeRetVal = default; // Setup - Perform required setup. int __retVal_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__numElements); @@ -40,6 +40,7 @@ finally { // Cleanup - Perform required cleanup. + __retVal_native__numElements = size; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); } @@ -53,8 +54,8 @@ var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::ComInterfaceGenerator.Tests.ComInterfaces.IIntArray)); System.Runtime.CompilerServices.Unsafe.SkipInit(out array); int* __array_native = default; - int __retVal; - int __invokeRetVal; + int __retVal = default; + int __invokeRetVal = default; // Setup - Perform required setup. int __array_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__numElements); @@ -74,6 +75,7 @@ finally { // Cleanup - Perform required cleanup. + __array_native__numElements = __retVal; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__array_native); } @@ -112,6 +114,7 @@ internal static int ABI_Get(System.Runtime.InteropServices.ComWrappers.ComInterf // Setup - Perform required setup. int __invokeRetValUnmanaged__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __invokeRetValUnmanaged__numElements); + ; try { // Unmarshal - Convert native data to managed data. @@ -143,6 +146,7 @@ internal static int ABI_Get2(System.Runtime.InteropServices.ComWrappers.ComInter // Setup - Perform required setup. int __array_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__numElements); + ; try { // Unmarshal - Convert native data to managed data. @@ -171,6 +175,7 @@ internal static int ABI_Set(System.Runtime.InteropServices.ComWrappers.ComInterf // Setup - Perform required setup. int __array_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__numElements); + ; try { // Unmarshal - Convert native data to managed data. diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArray.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArray.cs index 09b1660..e43ae92 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArray.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArray.cs @@ -18,9 +18,9 @@ System.Runtime.CompilerServices.Unsafe.SkipInit(out widths); System.Runtime.CompilerServices.Unsafe.SkipInit(out length); int* __widths_native = default; - int[][] __retVal; + int[][] __retVal = default; System.IntPtr* __retVal_native = default; - int __invokeRetVal; + int __invokeRetVal = default; // Setup - Perform required setup. int __widths_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __widths_native__numElements); @@ -49,6 +49,7 @@ { int __retVal_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__nativeSpan____i0__numElements); + ; __retVal_native__nativeSpan____i0__numElements = widths[__i0]; __retVal_native__managedSpan[__i0] = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements((int*)__retVal_native__nativeSpan[__i0], __retVal_native__nativeSpan____i0__numElements); global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource((int*)__retVal_native__nativeSpan[__i0], __retVal_native__nativeSpan____i0__numElements).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(__retVal_native__managedSpan[__i0])); @@ -58,13 +59,17 @@ finally { // Cleanup - Perform required cleanup. + __widths_native__numElements = length; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__widths_native); + __retVal_native__numElements = length; { System.ReadOnlySpan __retVal_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__retVal_native, __retVal_native__numElements); for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) { int __retVal_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__nativeSpan____i0__numElements); + ; + __retVal_native__nativeSpan____i0__numElements = widths[__i0]; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__retVal_native__nativeSpan[__i0]); } } @@ -84,8 +89,8 @@ System.Runtime.CompilerServices.Unsafe.SkipInit(out widths); System.IntPtr* __array_native = default; int* __widths_native = default; - int __retVal; - int __invokeRetVal; + int __retVal = default; + int __invokeRetVal = default; // Setup - Perform required setup. int __widths_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __widths_native__numElements); @@ -112,6 +117,7 @@ { int __array_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); + ; __array_native__nativeSpan____i0__numElements = widths[__i0]; __array_native__managedSpan[__i0] = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements((int*)__array_native__nativeSpan[__i0], __array_native__nativeSpan____i0__numElements); global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource((int*)__array_native__nativeSpan[__i0], __array_native__nativeSpan____i0__numElements).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(__array_native__managedSpan[__i0])); @@ -121,13 +127,17 @@ finally { // Cleanup - Perform required cleanup. + __widths_native__numElements = __retVal; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__widths_native); + __array_native__numElements = __retVal; { System.ReadOnlySpan __array_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__array_native, __array_native__numElements); for (int __i0 = 0; __i0 < __array_native__nativeSpan.Length; ++__i0) { int __array_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); + ; + __array_native__nativeSpan____i0__numElements = widths[__i0]; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__array_native__nativeSpan[__i0]); } } @@ -144,10 +154,11 @@ { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArray)); System.IntPtr* __array_native = default; - int __invokeRetVal; + int __invokeRetVal = default; // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn __array_native__marshaller = new(); int __array_native__numElements; + int __array_native__lastIndexMarshalled = 0; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__numElements); try { @@ -156,10 +167,11 @@ { System.ReadOnlySpan __array_native__managedSpan = __array_native__marshaller.GetManagedValuesSource(); System.Span __array_native__nativeSpan = __array_native__marshaller.GetUnmanagedValuesDestination(); - for (int __i0 = 0; __i0 < __array_native__managedSpan.Length; ++__i0) + for (int __i0 = 0; __i0 < __array_native__managedSpan.Length; ++__i0, ++__array_native__lastIndexMarshalled) { int __array_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); + ; __array_native__nativeSpan[__i0] = (System.IntPtr)global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(__array_native__managedSpan[__i0], out __array_native__nativeSpan____i0__numElements); global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(__array_native__managedSpan[__i0]).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination((int*)__array_native__nativeSpan[__i0], __array_native__nativeSpan____i0__numElements)); } @@ -183,10 +195,12 @@ // Cleanup - Perform required cleanup. { System.ReadOnlySpan __array_native__nativeSpan = __array_native__marshaller.GetUnmanagedValuesDestination(); - for (int __i0 = 0; __i0 < __array_native__nativeSpan.Length; ++__i0) + for (int __i0 = 0; __i0 < __array_native__lastIndexMarshalled; ++__i0) { int __array_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); + ; + ; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__array_native__nativeSpan[__i0]); } } @@ -212,8 +226,10 @@ internal static int ABI_Get(System.Runtime.InteropServices.ComWrappers.ComInterf // Setup - Perform required setup. int __widths_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __widths_native__numElements); + ; int __invokeRetValUnmanaged__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __invokeRetValUnmanaged__numElements); + ; try { // Unmarshal - Convert native data to managed data. @@ -232,6 +248,7 @@ internal static int ABI_Get(System.Runtime.InteropServices.ComWrappers.ComInterf { int __invokeRetValUnmanaged__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __invokeRetValUnmanaged__nativeSpan____i0__numElements); + ; __invokeRetValUnmanaged__nativeSpan[__i0] = (System.IntPtr)global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(__invokeRetValUnmanaged__managedSpan[__i0], out __invokeRetValUnmanaged__nativeSpan____i0__numElements); global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(__invokeRetValUnmanaged__managedSpan[__i0]).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination((int*)__invokeRetValUnmanaged__nativeSpan[__i0], __invokeRetValUnmanaged__nativeSpan____i0__numElements)); } @@ -259,8 +276,10 @@ internal static int ABI_Get2(System.Runtime.InteropServices.ComWrappers.ComInter // Setup - Perform required setup. int __widths_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __widths_native__numElements); + ; int __array_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__numElements); + ; try { // Unmarshal - Convert native data to managed data. @@ -279,6 +298,7 @@ internal static int ABI_Get2(System.Runtime.InteropServices.ComWrappers.ComInter { int __array_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); + ; __array_native__nativeSpan[__i0] = (System.IntPtr)global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(__array_native__managedSpan[__i0], out __array_native__nativeSpan____i0__numElements); global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(__array_native__managedSpan[__i0]).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination((int*)__array_native__nativeSpan[__i0], __array_native__nativeSpan____i0__numElements)); } @@ -302,8 +322,10 @@ internal static int ABI_Set(System.Runtime.InteropServices.ComWrappers.ComInterf // Setup - Perform required setup. int __widths_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __widths_native__numElements); + ; int __array_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__numElements); + ; try { // Unmarshal - Convert native data to managed data. @@ -320,6 +342,7 @@ internal static int ABI_Set(System.Runtime.InteropServices.ComWrappers.ComInterf { int __array_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); + ; __array_native__nativeSpan____i0__numElements = widths[__i0]; __array_native__managedSpan[__i0] = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements((int*)__array_native__nativeSpan[__i0], __array_native__nativeSpan____i0__numElements); global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource((int*)__array_native__nativeSpan[__i0], __array_native__nativeSpan____i0__numElements).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(__array_native__managedSpan[__i0])); diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArrayMarshallingFails.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArrayMarshallingFails.cs index b50969c..ecf521a 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArrayMarshallingFails.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArrayMarshallingFails.cs @@ -18,9 +18,9 @@ System.Runtime.CompilerServices.Unsafe.SkipInit(out widths); System.Runtime.CompilerServices.Unsafe.SkipInit(out length); int* __widths_native = default; - int[][] __retVal; + int[][] __retVal = default; System.IntPtr* __retVal_native = default; - int __invokeRetVal; + int __invokeRetVal = default; // Setup - Perform required setup. int __widths_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __widths_native__numElements); @@ -49,6 +49,7 @@ { int __retVal_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__nativeSpan____i0__numElements); + ; __retVal_native__nativeSpan____i0__numElements = widths[__i0]; __retVal_native__managedSpan[__i0] = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements((nint*)__retVal_native__nativeSpan[__i0], __retVal_native__nativeSpan____i0__numElements); { @@ -65,13 +66,17 @@ finally { // Cleanup - Perform required cleanup. + __widths_native__numElements = length; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__widths_native); + __retVal_native__numElements = length; { System.ReadOnlySpan __retVal_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__retVal_native, __retVal_native__numElements); for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) { int __retVal_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__nativeSpan____i0__numElements); + ; + __retVal_native__nativeSpan____i0__numElements = widths[__i0]; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__retVal_native__nativeSpan[__i0]); } } @@ -91,8 +96,8 @@ System.Runtime.CompilerServices.Unsafe.SkipInit(out widths); System.IntPtr* __array_native = default; int* __widths_native = default; - int __retVal; - int __invokeRetVal; + int __retVal = default; + int __invokeRetVal = default; // Setup - Perform required setup. int __widths_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __widths_native__numElements); @@ -119,6 +124,7 @@ { int __array_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); + ; __array_native__nativeSpan____i0__numElements = widths[__i0]; __array_native__managedSpan[__i0] = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements((nint*)__array_native__nativeSpan[__i0], __array_native__nativeSpan____i0__numElements); { @@ -135,13 +141,17 @@ finally { // Cleanup - Perform required cleanup. + __widths_native__numElements = __retVal; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__widths_native); + __array_native__numElements = __retVal; { System.ReadOnlySpan __array_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__array_native, __array_native__numElements); for (int __i0 = 0; __i0 < __array_native__nativeSpan.Length; ++__i0) { int __array_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); + ; + __array_native__nativeSpan____i0__numElements = widths[__i0]; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__array_native__nativeSpan[__i0]); } } @@ -158,10 +168,11 @@ { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArrayMarshallingFails)); System.IntPtr* __array_native = default; - int __invokeRetVal; + int __invokeRetVal = default; // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn __array_native__marshaller = new(); int __array_native__numElements; + int __array_native__lastIndexMarshalled = 0; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__numElements); try { @@ -170,10 +181,11 @@ { System.ReadOnlySpan __array_native__managedSpan = __array_native__marshaller.GetManagedValuesSource(); System.Span __array_native__nativeSpan = __array_native__marshaller.GetUnmanagedValuesDestination(); - for (int __i0 = 0; __i0 < __array_native__managedSpan.Length; ++__i0) + for (int __i0 = 0; __i0 < __array_native__managedSpan.Length; ++__i0, ++__array_native__lastIndexMarshalled) { int __array_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); + ; __array_native__nativeSpan[__i0] = (System.IntPtr)global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(__array_native__managedSpan[__i0], out __array_native__nativeSpan____i0__numElements); { System.ReadOnlySpan __array_native__nativeSpan____i0__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(__array_native__managedSpan[__i0]); @@ -204,10 +216,12 @@ // Cleanup - Perform required cleanup. { System.ReadOnlySpan __array_native__nativeSpan = __array_native__marshaller.GetUnmanagedValuesDestination(); - for (int __i0 = 0; __i0 < __array_native__nativeSpan.Length; ++__i0) + for (int __i0 = 0; __i0 < __array_native__lastIndexMarshalled; ++__i0) { int __array_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); + ; + ; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__array_native__nativeSpan[__i0]); } } @@ -233,8 +247,10 @@ internal static int ABI_Get(System.Runtime.InteropServices.ComWrappers.ComInterf // Setup - Perform required setup. int __widths_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __widths_native__numElements); + ; int __invokeRetValUnmanaged__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __invokeRetValUnmanaged__numElements); + ; try { // Unmarshal - Convert native data to managed data. @@ -253,6 +269,7 @@ internal static int ABI_Get(System.Runtime.InteropServices.ComWrappers.ComInterf { int __invokeRetValUnmanaged__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __invokeRetValUnmanaged__nativeSpan____i0__numElements); + ; __invokeRetValUnmanaged__nativeSpan[__i0] = (System.IntPtr)global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(__invokeRetValUnmanaged__managedSpan[__i0], out __invokeRetValUnmanaged__nativeSpan____i0__numElements); { System.ReadOnlySpan __invokeRetValUnmanaged__nativeSpan____i0__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(__invokeRetValUnmanaged__managedSpan[__i0]); @@ -287,8 +304,10 @@ internal static int ABI_Get2(System.Runtime.InteropServices.ComWrappers.ComInter // Setup - Perform required setup. int __widths_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __widths_native__numElements); + ; int __array_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__numElements); + ; try { // Unmarshal - Convert native data to managed data. @@ -307,6 +326,7 @@ internal static int ABI_Get2(System.Runtime.InteropServices.ComWrappers.ComInter { int __array_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); + ; __array_native__nativeSpan[__i0] = (System.IntPtr)global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(__array_native__managedSpan[__i0], out __array_native__nativeSpan____i0__numElements); { System.ReadOnlySpan __array_native__nativeSpan____i0__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(__array_native__managedSpan[__i0]); @@ -337,8 +357,10 @@ internal static int ABI_Set(System.Runtime.InteropServices.ComWrappers.ComInterf // Setup - Perform required setup. int __widths_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __widths_native__numElements); + ; int __array_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__numElements); + ; try { // Unmarshal - Convert native data to managed data. @@ -355,6 +377,7 @@ internal static int ABI_Set(System.Runtime.InteropServices.ComWrappers.ComInterf { int __array_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); + ; __array_native__nativeSpan____i0__numElements = widths[__i0]; __array_native__managedSpan[__i0] = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements((nint*)__array_native__nativeSpan[__i0], __array_native__nativeSpan____i0__numElements); { diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.ICustomStringMarshallingUtf16.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.ICustomStringMarshallingUtf16.cs index caa8f6d..5f934f9 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.ICustomStringMarshallingUtf16.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.ICustomStringMarshallingUtf16.cs @@ -15,9 +15,9 @@ string global::SharedTypes.ComInterfaces.ICustomStringMarshallingUtf16.GetString() { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.ICustomStringMarshallingUtf16)); - string __retVal; + string __retVal = default; ushort* __retVal_native = default; - int __invokeRetVal; + int __invokeRetVal = default; try { { diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IDerived.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IDerived.cs index 9db03af..6a913de 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IDerived.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IDerived.cs @@ -32,9 +32,9 @@ string global::SharedTypes.ComInterfaces.IDerived.GetName() { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IDerived)); - string __retVal; + string __retVal = default; ushort* __retVal_native = default; - int __invokeRetVal; + int __invokeRetVal = default; try { { diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IGetIntArray.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IGetIntArray.cs index 84291fd..f0ee47b 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IGetIntArray.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IGetIntArray.cs @@ -15,9 +15,9 @@ int[] global::SharedTypes.ComInterfaces.IGetIntArray.GetInts() { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IGetIntArray)); - int[] __retVal; + int[] __retVal = default; int* __retVal_native = default; - int __invokeRetVal; + int __invokeRetVal = default; // Setup - Perform required setup. int __retVal_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__numElements); @@ -37,6 +37,7 @@ finally { // Cleanup - Perform required cleanup. + __retVal_native__numElements = 10; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); } @@ -56,6 +57,7 @@ internal static int ABI_GetInts(System.Runtime.InteropServices.ComWrappers.ComIn // Setup - Perform required setup. int __invokeRetValUnmanaged__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __invokeRetValUnmanaged__numElements); + ; try { // Unmarshal - Convert native data to managed data. diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStringMarshallingOverride.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStringMarshallingOverride.cs index b9a438f..0cdca11 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStringMarshallingOverride.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStringMarshallingOverride.cs @@ -16,9 +16,9 @@ { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStringMarshallingOverride)); byte* __input_native = default; - string __retVal; + string __retVal = default; byte* __retVal_native = default; - int __invokeRetVal; + int __invokeRetVal = default; // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn __input_native__marshaller = new(); try @@ -51,9 +51,9 @@ string global::SharedTypes.ComInterfaces.IStringMarshallingOverride.MarshalAsLPWString(string input) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStringMarshallingOverride)); - string __retVal; + string __retVal = default; ushort* __retVal_native = default; - int __invokeRetVal; + int __invokeRetVal = default; try { // Pin - Pin data in preparation for calling the P/Invoke. @@ -81,9 +81,9 @@ string global::SharedTypes.ComInterfaces.IStringMarshallingOverride.MarshalUsingUtf16(string input) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStringMarshallingOverride)); - string __retVal; + string __retVal = default; ushort* __retVal_native = default; - int __invokeRetVal; + int __invokeRetVal = default; try { // Pin - Pin data in preparation for calling the P/Invoke. diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived.cs index 4e62b66..dacb7b2 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived.cs @@ -16,9 +16,9 @@ { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived)); byte* __input_native = default; - string __retVal; + string __retVal = default; byte* __retVal_native = default; - int __invokeRetVal; + int __invokeRetVal = default; // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn __input_native__marshaller = new(); try @@ -51,9 +51,9 @@ string global::SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived.MarshalAsLPWString_2(string input) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived)); - string __retVal; + string __retVal = default; ushort* __retVal_native = default; - int __invokeRetVal; + int __invokeRetVal = default; try { // Pin - Pin data in preparation for calling the P/Invoke. @@ -81,9 +81,9 @@ string global::SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived.MarshalUsingUtf16_2(string input) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived)); - string __retVal; + string __retVal = default; ushort* __retVal_native = default; - int __invokeRetVal; + int __invokeRetVal = default; try { // Pin - Pin data in preparation for calling the P/Invoke. @@ -112,9 +112,9 @@ { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived)); byte* __input_native = default; - string __retVal; + string __retVal = default; byte* __retVal_native = default; - int __invokeRetVal; + int __invokeRetVal = default; // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn __input_native__marshaller = new(); try @@ -147,9 +147,9 @@ string global::SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived.MarshalAsLPWString(string input) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived)); - string __retVal; + string __retVal = default; ushort* __retVal_native = default; - int __invokeRetVal; + int __invokeRetVal = default; try { // Pin - Pin data in preparation for calling the P/Invoke. @@ -177,9 +177,9 @@ string global::SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived.MarshalUsingUtf16(string input) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived)); - string __retVal; + string __retVal = default; ushort* __retVal_native = default; - int __invokeRetVal; + int __invokeRetVal = default; try { // Pin - Pin data in preparation for calling the P/Invoke. diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IUTF16Marshalling.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IUTF16Marshalling.cs index ac18e42..bd16ca8 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IUTF16Marshalling.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IUTF16Marshalling.cs @@ -15,9 +15,9 @@ string global::SharedTypes.ComInterfaces.IUTF16Marshalling.GetString() { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IUTF16Marshalling)); - string __retVal; + string __retVal = default; ushort* __retVal_native = default; - int __invokeRetVal; + int __invokeRetVal = default; try { { diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IUTF8Marshalling.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IUTF8Marshalling.cs index f6b468b..e36345c 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IUTF8Marshalling.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IUTF8Marshalling.cs @@ -15,9 +15,9 @@ string global::SharedTypes.ComInterfaces.IUTF8Marshalling.GetString() { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IUTF8Marshalling)); - string __retVal; + string __retVal = default; byte* __retVal_native = default; - int __invokeRetVal; + int __invokeRetVal = default; try { { @@ -44,7 +44,7 @@ { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IUTF8Marshalling)); byte* __value_native = default; - int __invokeRetVal; + int __invokeRetVal = default; // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn __value_native__marshaller = new(); try diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/ManagedToNativeStubs.g.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/ManagedToNativeStubs.g.cs index 93712ce..cdf04c0 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/ManagedToNativeStubs.g.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/ManagedToNativeStubs.g.cs @@ -151,6 +151,7 @@ internal unsafe partial interface Native finally { // Cleanup - Perform required cleanup. + __values_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__values_native); } } @@ -258,7 +259,7 @@ internal unsafe partial interface Native global::SharedTypes.IntWrapper global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObject.GetData() { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObject)); - global::SharedTypes.IntWrapper __retVal; + global::SharedTypes.IntWrapper __retVal = default; int __retVal_native = default; try { @@ -380,6 +381,7 @@ internal unsafe partial interface Native // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn __values_native__marshaller = new(); int __values_native__numElements; + int __values_native__lastIndexMarshalled = 0; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); try { @@ -388,7 +390,7 @@ internal unsafe partial interface Native { System.ReadOnlySpan __values_native__managedSpan = __values_native__marshaller.GetManagedValuesSource(); System.Span __values_native__nativeSpan = __values_native__marshaller.GetUnmanagedValuesDestination(); - for (int __i0 = 0; __i0 < __values_native__managedSpan.Length; ++__i0) + for (int __i0 = 0; __i0 < __values_native__managedSpan.Length; ++__i0, ++__values_native__lastIndexMarshalled) { __values_native__nativeSpan[__i0] = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToUnmanaged(__values_native__managedSpan[__i0]); } @@ -412,7 +414,7 @@ internal unsafe partial interface Native global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__oldValue_native); { System.ReadOnlySpan __values_native__nativeSpan = __values_native__marshaller.GetUnmanagedValuesDestination(); - for (int __i0 = 0; __i0 < __values_native__nativeSpan.Length; ++__i0) + for (int __i0 = 0; __i0 < __values_native__lastIndexMarshalled; ++__i0) { global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__values_native__nativeSpan[__i0]); } @@ -447,6 +449,7 @@ internal unsafe partial interface Native // Setup - Perform required setup. int __values_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); + int __values_native__lastIndexMarshalled = 0; try { // Marshal - Convert managed data to native data. @@ -454,7 +457,7 @@ internal unsafe partial interface Native { System.ReadOnlySpan __values_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(values); System.Span __values_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__values_native, __values_native__numElements); - for (int __i0 = 0; __i0 < __values_native__managedSpan.Length; ++__i0) + for (int __i0 = 0; __i0 < __values_native__managedSpan.Length; ++__i0, ++__values_native__lastIndexMarshalled) { __values_native__nativeSpan[__i0] = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToUnmanaged(__values_native__managedSpan[__i0]); } @@ -482,9 +485,10 @@ internal unsafe partial interface Native { // Cleanup - Perform required cleanup. global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__oldValue_native); + __values_native__numElements = numValues; { System.ReadOnlySpan __values_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__values_native, __values_native__numElements); - for (int __i0 = 0; __i0 < __values_native__nativeSpan.Length; ++__i0) + for (int __i0 = 0; __i0 < __values_native__lastIndexMarshalled; ++__i0) { global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__values_native__nativeSpan[__i0]); } @@ -517,6 +521,7 @@ internal unsafe partial interface Native // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn __values123_native__marshaller = new(); int __values123_native__numElements; + int __values123_native__lastIndexMarshalled = 0; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values123_native__numElements); try { @@ -525,7 +530,7 @@ internal unsafe partial interface Native { System.ReadOnlySpan __values123_native__managedSpan = __values123_native__marshaller.GetManagedValuesSource(); System.Span __values123_native__nativeSpan = __values123_native__marshaller.GetUnmanagedValuesDestination(); - for (int __i0 = 0; __i0 < __values123_native__managedSpan.Length; ++__i0) + for (int __i0 = 0; __i0 < __values123_native__managedSpan.Length; ++__i0, ++__values123_native__lastIndexMarshalled) { __values123_native__nativeSpan[__i0] = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToUnmanaged(__values123_native__managedSpan[__i0]); } @@ -556,7 +561,7 @@ internal unsafe partial interface Native // Cleanup - Perform required cleanup. { System.ReadOnlySpan __values123_native__nativeSpan = __values123_native__marshaller.GetUnmanagedValuesDestination(); - for (int __i0 = 0; __i0 < __values123_native__nativeSpan.Length; ++__i0) + for (int __i0 = 0; __i0 < __values123_native__lastIndexMarshalled; ++__i0) { global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__values123_native__nativeSpan[__i0]); } diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/NativeToManagedStubs.g.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/NativeToManagedStubs.g.cs index 77cd9c2..f4d5267 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/NativeToManagedStubs.g.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/NativeToManagedStubs.g.cs @@ -95,6 +95,7 @@ internal static void ABI_SumAndSetData(void* __this_native, int* __values_native // Setup - Perform required setup. int __values_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); + ; // Unmarshal - Convert native data to managed data. __values_native__numElements = numValues; values = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__values_native, __values_native__numElements); @@ -132,6 +133,7 @@ internal static void ABI_SumAndSetData(void* __this_native, int** __values_nativ System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); bool __values_native__ownOriginal = false; int* __values_native__original = __values_native; + ; try { // Unmarshal - Convert native data to managed data. @@ -151,6 +153,7 @@ internal static void ABI_SumAndSetData(void* __this_native, int** __values_nativ // Cleanup - Perform required cleanup. if (__values_native__ownOriginal) { + __values_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__values_native__original); } } @@ -178,6 +181,7 @@ internal static void ABI_MultiplyWithData(void* __this_native, int* __values_nat // Setup - Perform required setup. int __values_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); + ; // Unmarshal - Convert native data to managed data. __values_native__numElements = numValues; values = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__values_native, __values_native__numElements); @@ -306,6 +310,7 @@ internal static void ABI_SumAndSetData(void* __this_native, int* __values_native // Setup - Perform required setup. int __values_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); + ; // Unmarshal - Convert native data to managed data. __values_native__numElements = numValues; values = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__values_native, __values_native__numElements); @@ -351,6 +356,7 @@ internal static void ABI_SumAndSetData(void* __this_native, int** __values_nativ System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); bool __values_native__ownOriginal = false; int* __values_native__original = __values_native; + ; try { // Unmarshal - Convert native data to managed data. @@ -374,6 +380,7 @@ internal static void ABI_SumAndSetData(void* __this_native, int** __values_nativ { System.ReadOnlySpan __values_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(values); System.Span __values_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__values_native, __values_native__numElements); + __values_native__nativeSpan.Clear(); for (int __i0 = 0; __i0 < __values_native__managedSpan.Length; ++__i0) { __values_native__nativeSpan[__i0] = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToUnmanaged(__values_native__managedSpan[__i0]); @@ -385,6 +392,7 @@ internal static void ABI_SumAndSetData(void* __this_native, int** __values_nativ // Cleanup - Perform required cleanup. if (__values_native__ownOriginal) { + __values_native__numElements = numValues; { System.ReadOnlySpan __values_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__values_native__original, __values_native__numElements); for (int __i0 = 0; __i0 < __values_native__nativeSpan.Length; ++__i0) @@ -420,6 +428,7 @@ internal static void ABI_MultiplyWithData(void* __this_native, int* __values123_ // Setup - Perform required setup. int __values123_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values123_native__numElements); + ; // Unmarshal - Convert native data to managed data. __values123_native__numElements = numValues; values123 = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__values123_native, __values123_native__numElements); @@ -472,6 +481,7 @@ internal static void ABI_SumAndSetData(void* __this_native, int* __values_native // Setup - Perform required setup. global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.StatefulUnmanagedToManagedCollectionMarshaller.In __values_native__marshaller = new(); int __values_native__numElements; + ; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); try { @@ -528,6 +538,7 @@ internal static void ABI_SumAndSetData(void* __this_native, int** __values_nativ bool __values_native__ownOriginal = false; int* __values_native__original = __values_native; int __values_native__numElements; + ; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); try { @@ -556,6 +567,7 @@ internal static void ABI_SumAndSetData(void* __this_native, int** __values_nativ { System.ReadOnlySpan __values_native__managedSpan = __values_native__marshaller.GetManagedValuesSource(); System.Span __values_native__nativeSpan = __values_native__marshaller.GetUnmanagedValuesDestination(); + __values_native__nativeSpan.Clear(); for (int __i0 = 0; __i0 < __values_native__managedSpan.Length; ++__i0) { __values_native__nativeSpan[__i0] = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToUnmanaged(__values_native__managedSpan[__i0]); @@ -602,6 +614,7 @@ internal static void ABI_MultiplyWithData(void* __this_native, int* __values123_ // Setup - Perform required setup. global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.StatefulUnmanagedToManagedCollectionMarshaller.In __values123_native__marshaller = new(); int __values123_native__numElements; + ; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values123_native__numElements); try { diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs index 3e2b84f..9cf6893 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs @@ -332,10 +332,11 @@ public static partial int SumAndSetNativeObjectData(void* obj, ref int[] arr, in { System.Runtime.CompilerServices.Unsafe.SkipInit(out oldValue); int* __arr_native = default; - int __retVal; + int __retVal = default; // Setup - Perform required setup. int __arr_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __arr_native__numElements); + ; try { // Marshal - Convert managed data to native data. @@ -355,6 +356,7 @@ public static partial int SumAndSetNativeObjectData(void* obj, ref int[] arr, in finally { // Cleanup - Perform required cleanup. + __arr_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__arr_native); } diff --git a/LibraryImportGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs b/LibraryImportGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs index 173b04f..7e7c345 100644 --- a/LibraryImportGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs +++ b/LibraryImportGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs @@ -60,10 +60,11 @@ public unsafe partial class Arrays public static partial int SumInArray(in int[] values, int numValues) { int* __values_native = default; - int __retVal; + int __retVal = default; // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn __values_native__marshaller = new(); int __values_native__numElements; + ; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); try { @@ -106,6 +107,7 @@ public static partial void Duplicate(ref int[] values, int numValues) // Setup - Perform required setup. int __values_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); + ; try { // Marshal - Convert managed data to native data. @@ -123,6 +125,7 @@ public static partial void Duplicate(ref int[] values, int numValues) finally { // Cleanup - Perform required cleanup. + __values_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__values_native); } @@ -144,11 +147,12 @@ public unsafe partial class Arrays public static partial int[] CreateRange(int start, int end, out int numValues) { System.Runtime.CompilerServices.Unsafe.SkipInit(out numValues); - int[] __retVal; + int[] __retVal = default; int* __retVal_native = default; // Setup - Perform required setup. int __retVal_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__numElements); + ; try { // Pin - Pin data in preparation for calling the P/Invoke. @@ -165,6 +169,7 @@ public static partial int[] CreateRange(int start, int end, out int numValues) finally { // Cleanup - Perform required cleanup. + __retVal_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); } @@ -192,6 +197,7 @@ public static partial void CreateRange_Out(int start, int end, out int numValues // Setup - Perform required setup. int __res_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __res_native__numElements); + ; try { // Pin - Pin data in preparation for calling the P/Invoke. @@ -208,6 +214,7 @@ public static partial void CreateRange_Out(int start, int end, out int numValues finally { // Cleanup - Perform required cleanup. + __res_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__res_native); } @@ -280,6 +287,7 @@ public static partial void ReverseChars(ref char[] chars, int numElements) // Setup - Perform required setup. int __chars_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __chars_native__numElements); + ; try { // Marshal - Convert managed data to native data. @@ -297,6 +305,7 @@ public static partial void ReverseChars(ref char[] chars, int numElements) finally { // Cleanup - Perform required cleanup. + __chars_native__numElements = numElements; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__chars_native); } @@ -318,10 +327,11 @@ public unsafe partial class Arrays public static partial int SumStringLengths(string[] strArray) { System.IntPtr* __strArray_native = default; - int __retVal; + int __retVal = default; // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn __strArray_native__marshaller = new(); int __strArray_native__numElements; + int __strArray_native__lastIndexMarshalled = 0; System.Runtime.CompilerServices.Unsafe.SkipInit(out __strArray_native__numElements); try { @@ -330,7 +340,7 @@ public static partial int SumStringLengths(string[] strArray) { System.ReadOnlySpan __strArray_native__managedSpan = __strArray_native__marshaller.GetManagedValuesSource(); System.Span __strArray_native__nativeSpan = __strArray_native__marshaller.GetUnmanagedValuesDestination(); - for (int __i0 = 0; __i0 < __strArray_native__managedSpan.Length; ++__i0) + for (int __i0 = 0; __i0 < __strArray_native__managedSpan.Length; ++__i0, ++__strArray_native__lastIndexMarshalled) { __strArray_native__nativeSpan[__i0] = (System.IntPtr)(ushort*)global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.ConvertToUnmanaged(__strArray_native__managedSpan[__i0]); } @@ -349,7 +359,7 @@ public static partial int SumStringLengths(string[] strArray) // Cleanup - Perform required cleanup. { System.ReadOnlySpan __strArray_native__nativeSpan = __strArray_native__marshaller.GetUnmanagedValuesDestination(); - for (int __i0 = 0; __i0 < __strArray_native__nativeSpan.Length; ++__i0) + for (int __i0 = 0; __i0 < __strArray_native__lastIndexMarshalled; ++__i0) { global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free((ushort*)__strArray_native__nativeSpan[__i0]); } @@ -381,6 +391,7 @@ public static partial void ReverseStrings_Ref(ref string[] strArray, out int num // Setup - Perform required setup. int __strArray_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __strArray_native__numElements); + int __strArray_native__lastIndexMarshalled = 0; try { // Marshal - Convert managed data to native data. @@ -388,7 +399,7 @@ public static partial void ReverseStrings_Ref(ref string[] strArray, out int num { System.ReadOnlySpan __strArray_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(strArray); System.Span __strArray_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__strArray_native, __strArray_native__numElements); - for (int __i0 = 0; __i0 < __strArray_native__managedSpan.Length; ++__i0) + for (int __i0 = 0; __i0 < __strArray_native__managedSpan.Length; ++__i0, ++__strArray_native__lastIndexMarshalled) { __strArray_native__nativeSpan[__i0] = (System.IntPtr)(ushort*)global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.ConvertToUnmanaged(__strArray_native__managedSpan[__i0]); } @@ -415,9 +426,10 @@ public static partial void ReverseStrings_Ref(ref string[] strArray, out int num finally { // Cleanup - Perform required cleanup. + __strArray_native__numElements = numElements; { System.ReadOnlySpan __strArray_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__strArray_native, __strArray_native__numElements); - for (int __i0 = 0; __i0 < __strArray_native__nativeSpan.Length; ++__i0) + for (int __i0 = 0; __i0 < __strArray_native__lastIndexMarshalled; ++__i0) { global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free((ushort*)__strArray_native__nativeSpan[__i0]); } @@ -445,13 +457,15 @@ public static partial string[] ReverseStrings_Return(string[] strArray, out int { System.Runtime.CompilerServices.Unsafe.SkipInit(out numElements); System.IntPtr* __strArray_native = default; - string[] __retVal; + string[] __retVal = default; System.IntPtr* __retVal_native = default; // Setup - Perform required setup. int __retVal_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__numElements); + int __retVal_native__lastIndexMarshalled = 0; scoped global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn __strArray_native__marshaller = new(); int __strArray_native__numElements; + int __strArray_native__lastIndexMarshalled = 0; System.Runtime.CompilerServices.Unsafe.SkipInit(out __strArray_native__numElements); try { @@ -460,7 +474,7 @@ public static partial string[] ReverseStrings_Return(string[] strArray, out int { System.ReadOnlySpan __strArray_native__managedSpan = __strArray_native__marshaller.GetManagedValuesSource(); System.Span __strArray_native__nativeSpan = __strArray_native__marshaller.GetUnmanagedValuesDestination(); - for (int __i0 = 0; __i0 < __strArray_native__managedSpan.Length; ++__i0) + for (int __i0 = 0; __i0 < __strArray_native__managedSpan.Length; ++__i0, ++__strArray_native__lastIndexMarshalled) { __strArray_native__nativeSpan[__i0] = (System.IntPtr)(ushort*)global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.ConvertToUnmanaged(__strArray_native__managedSpan[__i0]); } @@ -490,9 +504,10 @@ public static partial string[] ReverseStrings_Return(string[] strArray, out int finally { // Cleanup - Perform required cleanup. + __retVal_native__numElements = numElements; { System.ReadOnlySpan __retVal_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__retVal_native, __retVal_native__numElements); - for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) + for (int __i0 = 0; __i0 < __retVal_native__lastIndexMarshalled; ++__i0) { global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free((ushort*)__retVal_native__nativeSpan[__i0]); } @@ -501,7 +516,7 @@ public static partial string[] ReverseStrings_Return(string[] strArray, out int global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); { System.ReadOnlySpan __strArray_native__nativeSpan = __strArray_native__marshaller.GetUnmanagedValuesDestination(); - for (int __i0 = 0; __i0 < __strArray_native__nativeSpan.Length; ++__i0) + for (int __i0 = 0; __i0 < __strArray_native__lastIndexMarshalled; ++__i0) { global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free((ushort*)__strArray_native__nativeSpan[__i0]); } @@ -535,8 +550,10 @@ public static partial void ReverseStrings_Out(string[] strArray, out int numElem // Setup - Perform required setup. int __res_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __res_native__numElements); + ; scoped global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn __strArray_native__marshaller = new(); int __strArray_native__numElements; + int __strArray_native__lastIndexMarshalled = 0; System.Runtime.CompilerServices.Unsafe.SkipInit(out __strArray_native__numElements); try { @@ -545,7 +562,7 @@ public static partial void ReverseStrings_Out(string[] strArray, out int numElem { System.ReadOnlySpan __strArray_native__managedSpan = __strArray_native__marshaller.GetManagedValuesSource(); System.Span __strArray_native__nativeSpan = __strArray_native__marshaller.GetUnmanagedValuesDestination(); - for (int __i0 = 0; __i0 < __strArray_native__managedSpan.Length; ++__i0) + for (int __i0 = 0; __i0 < __strArray_native__managedSpan.Length; ++__i0, ++__strArray_native__lastIndexMarshalled) { __strArray_native__nativeSpan[__i0] = (System.IntPtr)(ushort*)global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.ConvertToUnmanaged(__strArray_native__managedSpan[__i0]); } @@ -575,6 +592,7 @@ public static partial void ReverseStrings_Out(string[] strArray, out int numElem finally { // Cleanup - Perform required cleanup. + __res_native__numElements = numElements; { System.ReadOnlySpan __res_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__res_native, __res_native__numElements); for (int __i0 = 0; __i0 < __res_native__nativeSpan.Length; ++__i0) @@ -586,7 +604,7 @@ public static partial void ReverseStrings_Out(string[] strArray, out int numElem global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__res_native); { System.ReadOnlySpan __strArray_native__nativeSpan = __strArray_native__marshaller.GetUnmanagedValuesDestination(); - for (int __i0 = 0; __i0 < __strArray_native__nativeSpan.Length; ++__i0) + for (int __i0 = 0; __i0 < __strArray_native__lastIndexMarshalled; ++__i0) { global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free((ushort*)__strArray_native__nativeSpan[__i0]); } @@ -612,11 +630,12 @@ public unsafe partial class Arrays [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial byte[] GetLongBytes(long l) { - byte[] __retVal; + byte[] __retVal = default; byte* __retVal_native = default; // Setup - Perform required setup. int __retVal_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__numElements); + ; try { { @@ -631,6 +650,7 @@ public static partial byte[] GetLongBytes(long l) finally { // Cleanup - Perform required cleanup. + __retVal_native__numElements = 8; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); } @@ -656,6 +676,7 @@ public static partial void Append(ref int[] values, int numOriginalValues, int n // Setup - Perform required setup. int __values_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); + ; try { // Marshal - Convert managed data to native data. @@ -673,6 +694,7 @@ public static partial void Append(ref int[] values, int numOriginalValues, int n finally { // Cleanup - Perform required cleanup. + __values_native__numElements = checked(1 + numOriginalValues); global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__values_native); } @@ -694,11 +716,12 @@ public unsafe partial class Arrays public static partial bool FillRangeArray(global::SharedTypes.IntStructWrapper[] array, int length, int start) { global::SharedTypes.IntStructWrapperNative* __array_native = default; - bool __retVal; + bool __retVal = default; byte __retVal_native = default; // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn __array_native__marshaller = new(); int __array_native__numElements; + ; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__numElements); try { @@ -753,6 +776,7 @@ public static partial void DoubleValues(global::SharedTypes.IntStructWrapper[] a // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn __array_native__marshaller = new(); int __array_native__numElements; + ; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__numElements); try { @@ -810,11 +834,12 @@ public unsafe partial class Arrays public static partial bool AndAllMembers(global::SharedTypes.BoolStruct[] pArray, int length) { global::SharedTypes.BoolStructMarshaller.BoolStructNative* __pArray_native = default; - bool __retVal; + bool __retVal = default; byte __retVal_native = default; // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn __pArray_native__marshaller = new(); int __pArray_native__numElements; + ; System.Runtime.CompilerServices.Unsafe.SkipInit(out __pArray_native__numElements); try { @@ -865,11 +890,12 @@ public unsafe partial class Arrays public static partial bool AndAllMembersIn(in global::SharedTypes.BoolStruct[] pArray, int length) { global::SharedTypes.BoolStructMarshaller.BoolStructNative* __pArray_native = default; - bool __retVal; + bool __retVal = default; byte __retVal_native = default; // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn __pArray_native__marshaller = new(); int __pArray_native__numElements; + ; System.Runtime.CompilerServices.Unsafe.SkipInit(out __pArray_native__numElements); try { @@ -923,6 +949,7 @@ public static partial void NegateBools(ref global::SharedTypes.BoolStruct[] bool // Setup - Perform required setup. int __boolStruct_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __boolStruct_native__numElements); + ; try { // Marshal - Convert managed data to native data. @@ -955,6 +982,7 @@ public static partial void NegateBools(ref global::SharedTypes.BoolStruct[] bool finally { // Cleanup - Perform required cleanup. + __boolStruct_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__boolStruct_native); } @@ -981,8 +1009,10 @@ public static partial void NegateBools(global::SharedTypes.BoolStruct[] boolStru // Setup - Perform required setup. int __pBoolStructOut_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __pBoolStructOut_native__numElements); + ; scoped global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn __boolStruct_native__marshaller = new(); int __boolStruct_native__numElements; + ; System.Runtime.CompilerServices.Unsafe.SkipInit(out __boolStruct_native__numElements); try { @@ -1020,6 +1050,7 @@ public static partial void NegateBools(global::SharedTypes.BoolStruct[] boolStru finally { // Cleanup - Perform required cleanup. + __pBoolStructOut_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__pBoolStructOut_native); __boolStruct_native__marshaller.Free(); } @@ -1042,13 +1073,15 @@ public unsafe partial class Arrays public static partial global::SharedTypes.BoolStruct[] NegateBools(global::SharedTypes.BoolStruct[] boolStruct, int numValues) { global::SharedTypes.BoolStructMarshaller.BoolStructNative* __boolStruct_native = default; - global::SharedTypes.BoolStruct[] __retVal; + global::SharedTypes.BoolStruct[] __retVal = default; global::SharedTypes.BoolStructMarshaller.BoolStructNative* __retVal_native = default; // Setup - Perform required setup. int __retVal_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__numElements); + ; scoped global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn __boolStruct_native__marshaller = new(); int __boolStruct_native__numElements; + ; System.Runtime.CompilerServices.Unsafe.SkipInit(out __boolStruct_native__numElements); try { @@ -1086,6 +1119,7 @@ public unsafe partial class Arrays finally { // Cleanup - Perform required cleanup. + __retVal_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); __boolStruct_native__marshaller.Free(); } @@ -1109,13 +1143,15 @@ public unsafe partial class Arrays public static partial int[][] TransposeMatrix(int[][] matrix, int[] numRows, int numColumns) { System.IntPtr* __matrix_native = default; - int[][] __retVal; + int[][] __retVal = default; System.IntPtr* __retVal_native = default; // Setup - Perform required setup. int __retVal_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__numElements); + int __retVal_native__lastIndexMarshalled = 0; scoped global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn __matrix_native__marshaller = new(); int __matrix_native__numElements; + int __matrix_native__lastIndexMarshalled = 0; System.Runtime.CompilerServices.Unsafe.SkipInit(out __matrix_native__numElements); try { @@ -1124,10 +1160,11 @@ public static partial int[][] TransposeMatrix(int[][] matrix, int[] numRows, int { System.ReadOnlySpan __matrix_native__managedSpan = __matrix_native__marshaller.GetManagedValuesSource(); System.Span __matrix_native__nativeSpan = __matrix_native__marshaller.GetUnmanagedValuesDestination(); - for (int __i0 = 0; __i0 < __matrix_native__managedSpan.Length; ++__i0) + for (int __i0 = 0; __i0 < __matrix_native__managedSpan.Length; ++__i0, ++__matrix_native__lastIndexMarshalled) { int __matrix_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __matrix_native__nativeSpan____i0__numElements); + ; __matrix_native__nativeSpan[__i0] = (System.IntPtr)global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(__matrix_native__managedSpan[__i0], out __matrix_native__nativeSpan____i0__numElements); global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(__matrix_native__managedSpan[__i0]).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination((int*)__matrix_native__nativeSpan[__i0], __matrix_native__nativeSpan____i0__numElements)); } @@ -1152,6 +1189,7 @@ public static partial int[][] TransposeMatrix(int[][] matrix, int[] numRows, int { int __retVal_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__nativeSpan____i0__numElements); + ; __retVal_native__nativeSpan____i0__numElements = numRows[__i0]; __retVal_native__managedSpan[__i0] = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements((int*)__retVal_native__nativeSpan[__i0], __retVal_native__nativeSpan____i0__numElements); global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource((int*)__retVal_native__nativeSpan[__i0], __retVal_native__nativeSpan____i0__numElements).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(__retVal_native__managedSpan[__i0])); @@ -1161,12 +1199,15 @@ public static partial int[][] TransposeMatrix(int[][] matrix, int[] numRows, int finally { // Cleanup - Perform required cleanup. + __retVal_native__numElements = numColumns; { System.ReadOnlySpan __retVal_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__retVal_native, __retVal_native__numElements); - for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) + for (int __i0 = 0; __i0 < __retVal_native__lastIndexMarshalled; ++__i0) { int __retVal_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__nativeSpan____i0__numElements); + ; + __retVal_native__nativeSpan____i0__numElements = numRows[__i0]; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__retVal_native__nativeSpan[__i0]); } } @@ -1174,10 +1215,12 @@ public static partial int[][] TransposeMatrix(int[][] matrix, int[] numRows, int global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); { System.ReadOnlySpan __matrix_native__nativeSpan = __matrix_native__marshaller.GetUnmanagedValuesDestination(); - for (int __i0 = 0; __i0 < __matrix_native__nativeSpan.Length; ++__i0) + for (int __i0 = 0; __i0 < __matrix_native__lastIndexMarshalled; ++__i0) { int __matrix_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __matrix_native__nativeSpan____i0__numElements); + ; + ; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__matrix_native__nativeSpan[__i0]); } } @@ -1229,10 +1272,11 @@ public unsafe partial class Arrays public static unsafe partial int SumInArray(in int*[] values, int numValues) { nint* __values_native = default; - int __retVal; + int __retVal = default; // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.PointerArrayMarshaller.ManagedToUnmanagedIn __values_native__marshaller = new(); int __values_native__numElements; + ; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); try { @@ -1275,6 +1319,7 @@ public static unsafe partial void Duplicate(ref int*[] values, int numValues) // Setup - Perform required setup. int __values_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); + ; try { // Marshal - Convert managed data to native data. @@ -1292,6 +1337,7 @@ public static unsafe partial void Duplicate(ref int*[] values, int numValues) finally { // Cleanup - Perform required cleanup. + __values_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.PointerArrayMarshaller.Free(__values_native); } @@ -1312,11 +1358,12 @@ public unsafe partial class Arrays [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static unsafe partial int*[] ReturnDuplicate(int*[] values, int numValues) { - int*[] __retVal; + int*[] __retVal = default; nint* __retVal_native = default; // Setup - Perform required setup. int __retVal_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__numElements); + ; try { // Pin - Pin data in preparation for calling the P/Invoke. @@ -1333,6 +1380,7 @@ public unsafe partial class Arrays finally { // Cleanup - Perform required cleanup. + __retVal_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.PointerArrayMarshaller.Free(__retVal_native); } @@ -1960,20 +2008,812 @@ namespace LibraryImportGenerator.IntegrationTests { unsafe partial class NativeExportsNE { - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "42.42.42.42")] - [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] - public static partial void ReturnUIntAsUnicode_Out(uint input, out char res) + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "42.42.42.42")] + [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] + public static partial void ReturnUIntAsUnicode_Out(uint input, out char res) + { + System.Runtime.CompilerServices.Unsafe.SkipInit(out res); + // Pin - Pin data in preparation for calling the P/Invoke. + fixed (char* __res_native = &res) + { + __PInvoke(input, __res_native); + } + + // Local P/Invoke + [System.Runtime.InteropServices.DllImportAttribute("Microsoft.Interop.Tests.NativeExportsNE", EntryPoint = "char_return_as_refushort", ExactSpelling = true)] + static extern unsafe void __PInvoke(uint __input_native, char* __res_native); + } + } +} +namespace LibraryImportGenerator.IntegrationTests +{ + unsafe partial class NativeExportsNE + { + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "42.42.42.42")] + [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] + public static partial void ReturnUIntAsUnicode_In(uint input, in char res) + { + // Pin - Pin data in preparation for calling the P/Invoke. + fixed (char* __res_native = &res) + { + __PInvoke(input, __res_native); + } + + // Local P/Invoke + [System.Runtime.InteropServices.DllImportAttribute("Microsoft.Interop.Tests.NativeExportsNE", EntryPoint = "char_return_as_refushort", ExactSpelling = true)] + static extern unsafe void __PInvoke(uint __input_native, char* __res_native); + } + } +} +namespace LibraryImportGenerator.IntegrationTests +{ + unsafe partial class NativeExportsNE + { + [System.Runtime.InteropServices.DllImportAttribute("Microsoft.Interop.Tests.NativeExportsNE", EntryPoint = "char_return_as_uint", ExactSpelling = true)] + public static extern partial char ReturnU2AsU2IgnoreCharSet(char input); + } +} +namespace LibraryImportGenerator.IntegrationTests +{ + unsafe partial class NativeExportsNE + { + [System.Runtime.InteropServices.DllImportAttribute("Microsoft.Interop.Tests.NativeExportsNE", EntryPoint = "char_return_as_uint", ExactSpelling = true)] + public static extern partial char ReturnI2AsI2IgnoreCharSet(char input); + } +} +namespace LibraryImportGenerator.IntegrationTests +{ + unsafe partial class NativeExportsNE + { + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "42.42.42.42")] + [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] + public static partial void ReverseBuffer(ref char buffer, int len) + { + // Pin - Pin data in preparation for calling the P/Invoke. + fixed (char* __buffer_native = &buffer) + { + __PInvoke(__buffer_native, len); + } + + // Local P/Invoke + [System.Runtime.InteropServices.DllImportAttribute("Microsoft.Interop.Tests.NativeExportsNE", EntryPoint = "char_reverse_buffer_ref", ExactSpelling = true)] + static extern unsafe void __PInvoke(char* __buffer_native, int __len_native); + } + } +} +namespace LibraryImportGenerator.IntegrationTests +{ + unsafe partial class NativeExportsNE + { + public unsafe partial class MarshallingFails + { + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "42.42.42.42")] + [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] + public static partial void Utf8StringSpan(global::System.ReadOnlySpan s) + { + System.IntPtr* __s_native = default; + // Setup - Perform required setup. + scoped global::System.Runtime.InteropServices.Marshalling.ReadOnlySpanMarshaller.ManagedToUnmanagedIn __s_native__marshaller = new(); + int __s_native__numElements; + int __s_native__lastIndexMarshalled = 0; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __s_native__numElements); + try + { + // Marshal - Convert managed data to native data. + __s_native__marshaller.FromManaged(s, stackalloc System.IntPtr[global::System.Runtime.InteropServices.Marshalling.ReadOnlySpanMarshaller.ManagedToUnmanagedIn.BufferSize]); + { + System.ReadOnlySpan __s_native__managedSpan = __s_native__marshaller.GetManagedValuesSource(); + System.Span __s_native__nativeSpan = __s_native__marshaller.GetUnmanagedValuesDestination(); + for (int __i0 = 0; __i0 < __s_native__managedSpan.Length; ++__i0, ++__s_native__lastIndexMarshalled) + { + __s_native__nativeSpan[__i0] = (System.IntPtr)(byte*)global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ConvertToUnmanaged(__s_native__managedSpan[__i0]); + } + } + + // Pin - Pin data in preparation for calling the P/Invoke. + fixed (void* __s_native__unused = __s_native__marshaller) + { + // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. + __s_native = __s_native__marshaller.ToUnmanaged(); + __PInvoke(__s_native); + } + } + finally + { + // Cleanup - Perform required cleanup. + { + System.ReadOnlySpan __s_native__nativeSpan = __s_native__marshaller.GetUnmanagedValuesDestination(); + for (int __i0 = 0; __i0 < __s_native__lastIndexMarshalled; ++__i0) + { + global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.Free((byte*)__s_native__nativeSpan[__i0]); + } + } + + __s_native__marshaller.Free(); + } + + // Local P/Invoke + [System.Runtime.InteropServices.DllImportAttribute("DoesNotExist", EntryPoint = "Utf8StringSpan", ExactSpelling = true)] + static extern unsafe void __PInvoke(System.IntPtr* __s_native); + } + } + } +} +namespace LibraryImportGenerator.IntegrationTests +{ + unsafe partial class NativeExportsNE + { + public unsafe partial class MarshallingFails + { + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "42.42.42.42")] + [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] + public static partial void Utf8StringArray(string[] s) + { + System.IntPtr* __s_native = default; + // Setup - Perform required setup. + scoped global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn __s_native__marshaller = new(); + int __s_native__numElements; + int __s_native__lastIndexMarshalled = 0; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __s_native__numElements); + try + { + // Marshal - Convert managed data to native data. + __s_native__marshaller.FromManaged(s, stackalloc System.IntPtr[global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn.BufferSize]); + { + System.ReadOnlySpan __s_native__managedSpan = __s_native__marshaller.GetManagedValuesSource(); + System.Span __s_native__nativeSpan = __s_native__marshaller.GetUnmanagedValuesDestination(); + for (int __i0 = 0; __i0 < __s_native__managedSpan.Length; ++__i0, ++__s_native__lastIndexMarshalled) + { + __s_native__nativeSpan[__i0] = (System.IntPtr)(byte*)global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ConvertToUnmanaged(__s_native__managedSpan[__i0]); + } + } + + // Pin - Pin data in preparation for calling the P/Invoke. + fixed (void* __s_native__unused = __s_native__marshaller) + { + // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. + __s_native = __s_native__marshaller.ToUnmanaged(); + __PInvoke(__s_native); + } + } + finally + { + // Cleanup - Perform required cleanup. + { + System.ReadOnlySpan __s_native__nativeSpan = __s_native__marshaller.GetUnmanagedValuesDestination(); + for (int __i0 = 0; __i0 < __s_native__lastIndexMarshalled; ++__i0) + { + global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.Free((byte*)__s_native__nativeSpan[__i0]); + } + } + + __s_native__marshaller.Free(); + } + + // Local P/Invoke + [System.Runtime.InteropServices.DllImportAttribute("DoesNotExist", EntryPoint = "Utf8StringArray", ExactSpelling = true)] + static extern unsafe void __PInvoke(System.IntPtr* __s_native); + } + } + } +} +namespace LibraryImportGenerator.IntegrationTests +{ + unsafe partial class NativeExportsNE + { + public unsafe partial class MarshallingFails + { + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "42.42.42.42")] + [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] + public static partial void MarshalSingleDimensionalArray(global::SharedTypes.BoolStruct[] c) + { + nint* __c_native = default; + // Setup - Perform required setup. + scoped global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn __c_native__marshaller = new(); + int __c_native__numElements; + int __c_native__lastIndexMarshalled = 0; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __c_native__numElements); + try + { + // Marshal - Convert managed data to native data. + __c_native__marshaller.FromManaged(c, stackalloc nint[global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn.BufferSize]); + { + System.ReadOnlySpan __c_native__managedSpan = __c_native__marshaller.GetManagedValuesSource(); + System.Span __c_native__nativeSpan = __c_native__marshaller.GetUnmanagedValuesDestination(); + for (int __i0 = 0; __i0 < __c_native__managedSpan.Length; ++__i0, ++__c_native__lastIndexMarshalled) + { + __c_native__nativeSpan[__i0] = global::LibraryImportGenerator.IntegrationTests.EnforceLastElementMarshalledCleanupBoolStruct.ConvertToUnmanaged(__c_native__managedSpan[__i0]); + } + } + + // Pin - Pin data in preparation for calling the P/Invoke. + fixed (void* __c_native__unused = __c_native__marshaller) + { + // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. + __c_native = __c_native__marshaller.ToUnmanaged(); + __PInvoke(__c_native); + } + } + finally + { + // Cleanup - Perform required cleanup. + { + System.ReadOnlySpan __c_native__nativeSpan = __c_native__marshaller.GetUnmanagedValuesDestination(); + for (int __i0 = 0; __i0 < __c_native__lastIndexMarshalled; ++__i0) + { + global::LibraryImportGenerator.IntegrationTests.EnforceLastElementMarshalledCleanupBoolStruct.Free(__c_native__nativeSpan[__i0]); + } + } + + __c_native__marshaller.Free(); + } + + // Local P/Invoke + [System.Runtime.InteropServices.DllImportAttribute("DoesNotExist", EntryPoint = "MarshalSingleDimensionalArray", ExactSpelling = true)] + static extern unsafe void __PInvoke(nint* __c_native); + } + } + } +} +namespace LibraryImportGenerator.IntegrationTests +{ + unsafe partial class NativeExportsNE + { + public unsafe partial class MarshallingFails + { + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "42.42.42.42")] + [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] + public static partial void MarshalMultidimensionalArray_CheckOuterArrayIsIndexTracked(global::SharedTypes.BoolStruct[][] c) + { + nint* __c_native = default; + // Setup - Perform required setup. + scoped global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn __c_native__marshaller = new(); + int __c_native__numElements; + int __c_native__lastIndexMarshalled = 0; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __c_native__numElements); + try + { + // Marshal - Convert managed data to native data. + __c_native__marshaller.FromManaged(c, stackalloc nint[global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn.BufferSize]); + { + System.ReadOnlySpan __c_native__managedSpan = __c_native__marshaller.GetManagedValuesSource(); + System.Span __c_native__nativeSpan = __c_native__marshaller.GetUnmanagedValuesDestination(); + for (int __i0 = 0; __i0 < __c_native__managedSpan.Length; ++__i0, ++__c_native__lastIndexMarshalled) + { + __c_native__nativeSpan[__i0] = global::LibraryImportGenerator.IntegrationTests.EnforceLastElementMarshalledCleanupBoolStructArray.ConvertToUnmanaged(__c_native__managedSpan[__i0]); + } + } + + // Pin - Pin data in preparation for calling the P/Invoke. + fixed (void* __c_native__unused = __c_native__marshaller) + { + // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. + __c_native = __c_native__marshaller.ToUnmanaged(); + __PInvoke(__c_native); + } + } + finally + { + // Cleanup - Perform required cleanup. + { + System.ReadOnlySpan __c_native__nativeSpan = __c_native__marshaller.GetUnmanagedValuesDestination(); + for (int __i0 = 0; __i0 < __c_native__lastIndexMarshalled; ++__i0) + { + global::LibraryImportGenerator.IntegrationTests.EnforceLastElementMarshalledCleanupBoolStructArray.Free(__c_native__nativeSpan[__i0]); + } + } + + __c_native__marshaller.Free(); + } + + // Local P/Invoke + [System.Runtime.InteropServices.DllImportAttribute("DoesNotExist", EntryPoint = "MarshalMultidimensionalArray_CheckOuterArrayIsIndexTracked", ExactSpelling = true)] + static extern unsafe void __PInvoke(nint* __c_native); + } + } + } +} +namespace LibraryImportGenerator.IntegrationTests +{ + unsafe partial class NativeExportsNE + { + public unsafe partial class MarshallingFails + { + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "42.42.42.42")] + [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] + public static partial void MarshalMultidimensionalArray_CheckInnerArraysAreCleared(global::SharedTypes.BoolStruct[][] c) + { + System.IntPtr* __c_native = default; + // Setup - Perform required setup. + scoped global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn __c_native__marshaller = new(); + int __c_native__numElements; + int __c_native__lastIndexMarshalled = 0; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __c_native__numElements); + try + { + // Marshal - Convert managed data to native data. + __c_native__marshaller.FromManaged(c, stackalloc System.IntPtr[global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn.BufferSize]); + { + System.ReadOnlySpan __c_native__managedSpan = __c_native__marshaller.GetManagedValuesSource(); + System.Span __c_native__nativeSpan = __c_native__marshaller.GetUnmanagedValuesDestination(); + for (int __i0 = 0; __i0 < __c_native__managedSpan.Length; ++__i0, ++__c_native__lastIndexMarshalled) + { + int __c_native__nativeSpan____i0__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __c_native__nativeSpan____i0__numElements); + ; + __c_native__nativeSpan[__i0] = (System.IntPtr)global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(__c_native__managedSpan[__i0], out __c_native__nativeSpan____i0__numElements); + { + System.ReadOnlySpan __c_native__nativeSpan____i0__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(__c_native__managedSpan[__i0]); + System.Span __c_native__nativeSpan____i0__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination((nint*)__c_native__nativeSpan[__i0], __c_native__nativeSpan____i0__numElements); + for (int __i1 = 0; __i1 < __c_native__nativeSpan____i0__managedSpan.Length; ++__i1) + { + __c_native__nativeSpan____i0__nativeSpan[__i1] = global::LibraryImportGenerator.IntegrationTests.EnforceClearedMemoryCleanup.ConvertToUnmanaged(__c_native__nativeSpan____i0__managedSpan[__i1]); + } + } + } + } + + // Pin - Pin data in preparation for calling the P/Invoke. + fixed (void* __c_native__unused = __c_native__marshaller) + { + // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. + __c_native = __c_native__marshaller.ToUnmanaged(); + __PInvoke(__c_native); + } + } + finally + { + // Cleanup - Perform required cleanup. + { + System.ReadOnlySpan __c_native__nativeSpan = __c_native__marshaller.GetUnmanagedValuesDestination(); + for (int __i0 = 0; __i0 < __c_native__lastIndexMarshalled; ++__i0) + { + int __c_native__nativeSpan____i0__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __c_native__nativeSpan____i0__numElements); + ; + ; + { + System.ReadOnlySpan __c_native__nativeSpan____i0__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination((nint*)__c_native__nativeSpan[__i0], __c_native__nativeSpan____i0__numElements); + for (int __i1 = 0; __i1 < __c_native__nativeSpan____i0__nativeSpan.Length; ++__i1) + { + global::LibraryImportGenerator.IntegrationTests.EnforceClearedMemoryCleanup.Free(__c_native__nativeSpan____i0__nativeSpan[__i1]); + } + } + + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__c_native__nativeSpan[__i0]); + } + } + + __c_native__marshaller.Free(); + } + + // Local P/Invoke + [System.Runtime.InteropServices.DllImportAttribute("DoesNotExist", EntryPoint = "MarshalMultidimensionalArray_CheckInnerArraysAreCleared", ExactSpelling = true)] + static extern unsafe void __PInvoke(System.IntPtr* __c_native); + } + } + } +} +namespace LibraryImportGenerator.IntegrationTests +{ + unsafe partial class NativeExportsNE + { + public unsafe partial class MarshallingFails + { + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "42.42.42.42")] + [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] + public static partial void MarshalArray_Ref(ref global::SharedTypes.BoolStruct[] c) + { + nint* __c_native = default; + // Setup - Perform required setup. + int __c_native__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __c_native__numElements); + int __c_native__lastIndexMarshalled = 0; + try + { + // Marshal - Convert managed data to native data. + __c_native = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(c, out __c_native__numElements); + { + System.ReadOnlySpan __c_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(c); + System.Span __c_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__c_native, __c_native__numElements); + for (int __i0 = 0; __i0 < __c_native__managedSpan.Length; ++__i0, ++__c_native__lastIndexMarshalled) + { + __c_native__nativeSpan[__i0] = global::LibraryImportGenerator.IntegrationTests.EnforceLastElementMarshalledCleanupBoolStruct.ConvertToUnmanaged(__c_native__managedSpan[__i0]); + } + } + + { + __PInvoke(&__c_native); + } + + // Unmarshal - Convert native data to managed data. + __c_native__numElements = 10; + c = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__c_native, __c_native__numElements); + { + System.ReadOnlySpan __c_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__c_native, __c_native__numElements); + System.Span __c_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(c); + for (int __i0 = 0; __i0 < __c_native__numElements; ++__i0) + { + __c_native__managedSpan[__i0] = global::LibraryImportGenerator.IntegrationTests.EnforceLastElementMarshalledCleanupBoolStruct.ConvertToManaged(__c_native__nativeSpan[__i0]); + } + } + } + finally + { + // Cleanup - Perform required cleanup. + __c_native__numElements = 10; + { + System.ReadOnlySpan __c_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__c_native, __c_native__numElements); + for (int __i0 = 0; __i0 < __c_native__lastIndexMarshalled; ++__i0) + { + global::LibraryImportGenerator.IntegrationTests.EnforceLastElementMarshalledCleanupBoolStruct.Free(__c_native__nativeSpan[__i0]); + } + } + + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__c_native); + } + + // Local P/Invoke + [System.Runtime.InteropServices.DllImportAttribute("DoesNotExist", EntryPoint = "MarshalArray_Ref", ExactSpelling = true)] + static extern unsafe void __PInvoke(nint** __c_native); + } + } + } +} +namespace LibraryImportGenerator.IntegrationTests +{ + unsafe partial class NativeExportsNE + { + public unsafe partial class MarshallingFails + { + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "42.42.42.42")] + [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] + public static partial void NegateBoolsOut(global::SharedTypes.BoolStruct[] boolStruct, int numValues, out global::SharedTypes.BoolStruct[] pBoolStructOut) + { + System.Runtime.CompilerServices.Unsafe.SkipInit(out pBoolStructOut); + global::SharedTypes.BoolStructMarshaller.BoolStructNative* __boolStruct_native = default; + nint* __pBoolStructOut_native = default; + // Setup - Perform required setup. + int __pBoolStructOut_native__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __pBoolStructOut_native__numElements); + ; + scoped global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn __boolStruct_native__marshaller = new(); + int __boolStruct_native__numElements; + ; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __boolStruct_native__numElements); + try + { + // Marshal - Convert managed data to native data. + __boolStruct_native__marshaller.FromManaged(boolStruct, stackalloc global::SharedTypes.BoolStructMarshaller.BoolStructNative[global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn.BufferSize]); + { + System.ReadOnlySpan __boolStruct_native__managedSpan = __boolStruct_native__marshaller.GetManagedValuesSource(); + System.Span __boolStruct_native__nativeSpan = __boolStruct_native__marshaller.GetUnmanagedValuesDestination(); + for (int __i0 = 0; __i0 < __boolStruct_native__managedSpan.Length; ++__i0) + { + __boolStruct_native__nativeSpan[__i0] = global::SharedTypes.BoolStructMarshaller.ConvertToUnmanaged(__boolStruct_native__managedSpan[__i0]); + } + } + + // Pin - Pin data in preparation for calling the P/Invoke. + fixed (void* __boolStruct_native__unused = __boolStruct_native__marshaller) + { + // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. + __boolStruct_native = __boolStruct_native__marshaller.ToUnmanaged(); + __PInvoke(__boolStruct_native, numValues, &__pBoolStructOut_native); + } + + // Unmarshal - Convert native data to managed data. + __pBoolStructOut_native__numElements = numValues; + pBoolStructOut = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__pBoolStructOut_native, __pBoolStructOut_native__numElements); + { + System.ReadOnlySpan __pBoolStructOut_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__pBoolStructOut_native, __pBoolStructOut_native__numElements); + System.Span __pBoolStructOut_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(pBoolStructOut); + for (int __i0 = 0; __i0 < __pBoolStructOut_native__numElements; ++__i0) + { + __pBoolStructOut_native__managedSpan[__i0] = global::LibraryImportGenerator.IntegrationTests.EnforceAllElementsCleanedUpBoolStruct.ConvertToManaged(__pBoolStructOut_native__nativeSpan[__i0]); + } + } + } + finally + { + // Cleanup - Perform required cleanup. + __pBoolStructOut_native__numElements = numValues; + { + System.ReadOnlySpan __pBoolStructOut_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__pBoolStructOut_native, __pBoolStructOut_native__numElements); + for (int __i0 = 0; __i0 < __pBoolStructOut_native__nativeSpan.Length; ++__i0) + { + global::LibraryImportGenerator.IntegrationTests.EnforceAllElementsCleanedUpBoolStruct.Free(__pBoolStructOut_native__nativeSpan[__i0]); + } + } + + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__pBoolStructOut_native); + __boolStruct_native__marshaller.Free(); + } + + // Local P/Invoke + [System.Runtime.InteropServices.DllImportAttribute("Microsoft.Interop.Tests.NativeExportsNE", EntryPoint = "negate_bool_struct_array_out", ExactSpelling = true)] + static extern unsafe void __PInvoke(global::SharedTypes.BoolStructMarshaller.BoolStructNative* __boolStruct_native, int __numValues_native, nint** __pBoolStructOut_native); + } + } + } +} +namespace LibraryImportGenerator.IntegrationTests +{ + unsafe partial class NativeExportsNE + { + public unsafe partial class MarshallingFails + { + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "42.42.42.42")] + [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] + public static partial void NegateBoolsOut2D(global::SharedTypes.BoolStruct[][] boolStruct, int length, int[] widths, out global::SharedTypes.BoolStruct[][] pBoolStructOut) + { + System.Runtime.CompilerServices.Unsafe.SkipInit(out pBoolStructOut); + System.IntPtr* __boolStruct_native = default; + System.IntPtr* __pBoolStructOut_native = default; + // Setup - Perform required setup. + int __pBoolStructOut_native__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __pBoolStructOut_native__numElements); + ; + scoped global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn __boolStruct_native__marshaller = new(); + int __boolStruct_native__numElements; + int __boolStruct_native__lastIndexMarshalled = 0; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __boolStruct_native__numElements); + try + { + // Marshal - Convert managed data to native data. + __boolStruct_native__marshaller.FromManaged(boolStruct, stackalloc System.IntPtr[global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn.BufferSize]); + { + System.ReadOnlySpan __boolStruct_native__managedSpan = __boolStruct_native__marshaller.GetManagedValuesSource(); + System.Span __boolStruct_native__nativeSpan = __boolStruct_native__marshaller.GetUnmanagedValuesDestination(); + for (int __i0 = 0; __i0 < __boolStruct_native__managedSpan.Length; ++__i0, ++__boolStruct_native__lastIndexMarshalled) + { + int __boolStruct_native__nativeSpan____i0__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __boolStruct_native__nativeSpan____i0__numElements); + ; + __boolStruct_native__nativeSpan[__i0] = (System.IntPtr)global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(__boolStruct_native__managedSpan[__i0], out __boolStruct_native__nativeSpan____i0__numElements); + { + System.ReadOnlySpan __boolStruct_native__nativeSpan____i0__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(__boolStruct_native__managedSpan[__i0]); + System.Span __boolStruct_native__nativeSpan____i0__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination((global::SharedTypes.BoolStructMarshaller.BoolStructNative*)__boolStruct_native__nativeSpan[__i0], __boolStruct_native__nativeSpan____i0__numElements); + for (int __i1 = 0; __i1 < __boolStruct_native__nativeSpan____i0__managedSpan.Length; ++__i1) + { + __boolStruct_native__nativeSpan____i0__nativeSpan[__i1] = global::SharedTypes.BoolStructMarshaller.ConvertToUnmanaged(__boolStruct_native__nativeSpan____i0__managedSpan[__i1]); + } + } + } + } + + // Pin - Pin data in preparation for calling the P/Invoke. + fixed (void* __widths_native = &global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn.GetPinnableReference(widths)) + fixed (void* __boolStruct_native__unused = __boolStruct_native__marshaller) + { + // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. + __boolStruct_native = __boolStruct_native__marshaller.ToUnmanaged(); + __PInvoke(__boolStruct_native, length, (int*)__widths_native, &__pBoolStructOut_native); + } + + // Unmarshal - Convert native data to managed data. + __pBoolStructOut_native__numElements = length; + pBoolStructOut = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__pBoolStructOut_native, __pBoolStructOut_native__numElements); + { + System.ReadOnlySpan __pBoolStructOut_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__pBoolStructOut_native, __pBoolStructOut_native__numElements); + System.Span __pBoolStructOut_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(pBoolStructOut); + for (int __i0 = 0; __i0 < __pBoolStructOut_native__numElements; ++__i0) + { + int __pBoolStructOut_native__nativeSpan____i0__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __pBoolStructOut_native__nativeSpan____i0__numElements); + ; + __pBoolStructOut_native__nativeSpan____i0__numElements = widths[__i0]; + __pBoolStructOut_native__managedSpan[__i0] = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements((nint*)__pBoolStructOut_native__nativeSpan[__i0], __pBoolStructOut_native__nativeSpan____i0__numElements); + { + System.ReadOnlySpan __pBoolStructOut_native__nativeSpan____i0__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource((nint*)__pBoolStructOut_native__nativeSpan[__i0], __pBoolStructOut_native__nativeSpan____i0__numElements); + System.Span __pBoolStructOut_native__nativeSpan____i0__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(__pBoolStructOut_native__managedSpan[__i0]); + for (int __i1 = 0; __i1 < __pBoolStructOut_native__nativeSpan____i0__numElements; ++__i1) + { + __pBoolStructOut_native__nativeSpan____i0__managedSpan[__i1] = global::LibraryImportGenerator.IntegrationTests.EnforceAllElementsCleanedUpBoolStruct.ConvertToManaged(__pBoolStructOut_native__nativeSpan____i0__nativeSpan[__i1]); + } + } + } + } + } + finally + { + // Cleanup - Perform required cleanup. + __pBoolStructOut_native__numElements = length; + { + System.ReadOnlySpan __pBoolStructOut_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__pBoolStructOut_native, __pBoolStructOut_native__numElements); + for (int __i0 = 0; __i0 < __pBoolStructOut_native__nativeSpan.Length; ++__i0) + { + int __pBoolStructOut_native__nativeSpan____i0__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __pBoolStructOut_native__nativeSpan____i0__numElements); + ; + __pBoolStructOut_native__nativeSpan____i0__numElements = widths[__i0]; + { + System.ReadOnlySpan __pBoolStructOut_native__nativeSpan____i0__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination((nint*)__pBoolStructOut_native__nativeSpan[__i0], __pBoolStructOut_native__nativeSpan____i0__numElements); + for (int __i1 = 0; __i1 < __pBoolStructOut_native__nativeSpan____i0__nativeSpan.Length; ++__i1) + { + global::LibraryImportGenerator.IntegrationTests.EnforceAllElementsCleanedUpBoolStruct.Free(__pBoolStructOut_native__nativeSpan____i0__nativeSpan[__i1]); + } + } + + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__pBoolStructOut_native__nativeSpan[__i0]); + } + } + + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__pBoolStructOut_native); + { + System.ReadOnlySpan __boolStruct_native__nativeSpan = __boolStruct_native__marshaller.GetUnmanagedValuesDestination(); + for (int __i0 = 0; __i0 < __boolStruct_native__lastIndexMarshalled; ++__i0) + { + int __boolStruct_native__nativeSpan____i0__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __boolStruct_native__nativeSpan____i0__numElements); + ; + ; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((global::SharedTypes.BoolStructMarshaller.BoolStructNative*)__boolStruct_native__nativeSpan[__i0]); + } + } + + __boolStruct_native__marshaller.Free(); + } + + // Local P/Invoke + [System.Runtime.InteropServices.DllImportAttribute("Microsoft.Interop.Tests.NativeExportsNE", EntryPoint = "negate_bool_struct_array_out_2d", ExactSpelling = true)] + static extern unsafe void __PInvoke(System.IntPtr* __boolStruct_native, int __length_native, int* __widths_native, System.IntPtr** __pBoolStructOut_native); + } + } + } +} +namespace LibraryImportGenerator.IntegrationTests +{ + unsafe partial class NativeExportsNE + { + public unsafe partial class MarshallingFails + { + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "42.42.42.42")] + [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] + public static partial bool FillRangeArray(global::SharedTypes.IntStructWrapper[] array, int length, int start) + { + nint* __array_native = default; + bool __retVal = default; + byte __retVal_native = default; + // Setup - Perform required setup. + scoped global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn __array_native__marshaller = new(); + int __array_native__numElements; + ; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__numElements); + try + { + // Marshal - Convert managed data to native data. + __array_native__marshaller.FromManaged(array, stackalloc nint[global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn.BufferSize]); + __array_native__marshaller.GetUnmanagedValuesDestination().Clear(); + // Pin - Pin data in preparation for calling the P/Invoke. + fixed (void* __array_native__unused = __array_native__marshaller) + { + // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. + __array_native = __array_native__marshaller.ToUnmanaged(); + __retVal_native = __PInvoke(__array_native, length, start); + } + + // Unmarshal - Convert native data to managed data. + __retVal = __retVal_native != 0; + { + __array_native__numElements = __array_native__marshaller.GetManagedValuesSource().Length; + System.Span __array_native__managedSpan = System.Runtime.InteropServices.MemoryMarshal.CreateSpan(ref System.Runtime.CompilerServices.Unsafe.AsRef(in __array_native__marshaller.GetManagedValuesSource().GetPinnableReference()), __array_native__numElements); + System.Span __array_native__nativeSpan = __array_native__marshaller.GetUnmanagedValuesDestination(); + for (int __i0 = 0; __i0 < __array_native__numElements; ++__i0) + { + __array_native__managedSpan[__i0] = global::LibraryImportGenerator.IntegrationTests.EnforceAllElementsCleanedUpIntStruct.ConvertToManaged(__array_native__nativeSpan[__i0]); + } + } + } + finally + { + // Cleanup - Perform required cleanup. + { + System.ReadOnlySpan __array_native__nativeSpan = __array_native__marshaller.GetUnmanagedValuesDestination(); + for (int __i0 = 0; __i0 < __array_native__nativeSpan.Length; ++__i0) + { + global::LibraryImportGenerator.IntegrationTests.EnforceAllElementsCleanedUpIntStruct.Free(__array_native__nativeSpan[__i0]); + } + } + + __array_native__marshaller.Free(); + } + + return __retVal; + // Local P/Invoke + [System.Runtime.InteropServices.DllImportAttribute("Microsoft.Interop.Tests.NativeExportsNE", EntryPoint = "fill_range_array", ExactSpelling = true)] + static extern unsafe byte __PInvoke(nint* __array_native, int __length_native, int __start_native); + } + } + } +} +namespace LibraryImportGenerator.IntegrationTests +{ + unsafe partial class NativeExportsNE + { + public unsafe partial class MarshallingFails { - System.Runtime.CompilerServices.Unsafe.SkipInit(out res); - // Pin - Pin data in preparation for calling the P/Invoke. - fixed (char* __res_native = &res) + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "42.42.42.42")] + [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] + public static partial bool FillRangeArray2D(global::SharedTypes.IntStructWrapper[][] array, int length, int[] widths, int start) { - __PInvoke(input, __res_native); - } + System.IntPtr* __array_native = default; + bool __retVal = default; + byte __retVal_native = default; + // Setup - Perform required setup. + scoped global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn __array_native__marshaller = new(); + int __array_native__numElements; + ; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__numElements); + try + { + // Marshal - Convert managed data to native data. + __array_native__marshaller.FromManaged(array, stackalloc System.IntPtr[global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn.BufferSize]); + __array_native__marshaller.GetUnmanagedValuesDestination().Clear(); + // Pin - Pin data in preparation for calling the P/Invoke. + fixed (void* __widths_native = &global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn.GetPinnableReference(widths)) + fixed (void* __array_native__unused = __array_native__marshaller) + { + // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. + __array_native = __array_native__marshaller.ToUnmanaged(); + __retVal_native = __PInvoke(__array_native, length, (int*)__widths_native, start); + } - // Local P/Invoke - [System.Runtime.InteropServices.DllImportAttribute("Microsoft.Interop.Tests.NativeExportsNE", EntryPoint = "char_return_as_refushort", ExactSpelling = true)] - static extern unsafe void __PInvoke(uint __input_native, char* __res_native); + // Unmarshal - Convert native data to managed data. + __retVal = __retVal_native != 0; + { + __array_native__numElements = __array_native__marshaller.GetManagedValuesSource().Length; + System.Span __array_native__managedSpan = System.Runtime.InteropServices.MemoryMarshal.CreateSpan(ref System.Runtime.CompilerServices.Unsafe.AsRef(in __array_native__marshaller.GetManagedValuesSource().GetPinnableReference()), __array_native__numElements); + System.Span __array_native__nativeSpan = __array_native__marshaller.GetUnmanagedValuesDestination(); + for (int __i0 = 0; __i0 < __array_native__numElements; ++__i0) + { + int __array_native__nativeSpan____i0__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); + ; + __array_native__nativeSpan____i0__numElements = widths[__i0]; + __array_native__managedSpan[__i0] = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements((nint*)__array_native__nativeSpan[__i0], __array_native__nativeSpan____i0__numElements); + { + System.ReadOnlySpan __array_native__nativeSpan____i0__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource((nint*)__array_native__nativeSpan[__i0], __array_native__nativeSpan____i0__numElements); + System.Span __array_native__nativeSpan____i0__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(__array_native__managedSpan[__i0]); + for (int __i1 = 0; __i1 < __array_native__nativeSpan____i0__numElements; ++__i1) + { + __array_native__nativeSpan____i0__managedSpan[__i1] = global::LibraryImportGenerator.IntegrationTests.EnforceAllElementsCleanedUpIntStruct.ConvertToManaged(__array_native__nativeSpan____i0__nativeSpan[__i1]); + } + } + } + } + } + finally + { + // Cleanup - Perform required cleanup. + { + System.ReadOnlySpan __array_native__nativeSpan = __array_native__marshaller.GetUnmanagedValuesDestination(); + for (int __i0 = 0; __i0 < __array_native__nativeSpan.Length; ++__i0) + { + int __array_native__nativeSpan____i0__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); + ; + __array_native__nativeSpan____i0__numElements = widths[__i0]; + { + System.ReadOnlySpan __array_native__nativeSpan____i0__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination((nint*)__array_native__nativeSpan[__i0], __array_native__nativeSpan____i0__numElements); + for (int __i1 = 0; __i1 < __array_native__nativeSpan____i0__nativeSpan.Length; ++__i1) + { + global::LibraryImportGenerator.IntegrationTests.EnforceAllElementsCleanedUpIntStruct.Free(__array_native__nativeSpan____i0__nativeSpan[__i1]); + } + } + + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__array_native__nativeSpan[__i0]); + } + } + + __array_native__marshaller.Free(); + } + + return __retVal; + // Local P/Invoke + [System.Runtime.InteropServices.DllImportAttribute("Microsoft.Interop.Tests.NativeExportsNE", EntryPoint = "fill_range_array_2d", ExactSpelling = true)] + static extern unsafe byte __PInvoke(System.IntPtr* __array_native, int __length_native, int* __widths_native, int __start_native); + } } } } @@ -1981,19 +2821,65 @@ namespace LibraryImportGenerator.IntegrationTests { unsafe partial class NativeExportsNE { - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "42.42.42.42")] - [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] - public static partial void ReturnUIntAsUnicode_In(uint input, in char res) + public unsafe partial class MarshallingFails { - // Pin - Pin data in preparation for calling the P/Invoke. - fixed (char* __res_native = &res) + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "42.42.42.42")] + [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] + public static partial void NegateBoolsRef(ref global::SharedTypes.BoolStruct[] boolStruct, int numValues) { - __PInvoke(input, __res_native); - } + nint* __boolStruct_native = default; + // Setup - Perform required setup. + int __boolStruct_native__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __boolStruct_native__numElements); + int __boolStruct_native__lastIndexMarshalled = 0; + try + { + // Marshal - Convert managed data to native data. + __boolStruct_native = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(boolStruct, out __boolStruct_native__numElements); + { + System.ReadOnlySpan __boolStruct_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(boolStruct); + System.Span __boolStruct_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__boolStruct_native, __boolStruct_native__numElements); + for (int __i0 = 0; __i0 < __boolStruct_native__managedSpan.Length; ++__i0, ++__boolStruct_native__lastIndexMarshalled) + { + __boolStruct_native__nativeSpan[__i0] = global::LibraryImportGenerator.IntegrationTests.EnforceLastElementMarshalledCleanupBoolStruct.ConvertToUnmanaged(__boolStruct_native__managedSpan[__i0]); + } + } - // Local P/Invoke - [System.Runtime.InteropServices.DllImportAttribute("Microsoft.Interop.Tests.NativeExportsNE", EntryPoint = "char_return_as_refushort", ExactSpelling = true)] - static extern unsafe void __PInvoke(uint __input_native, char* __res_native); + { + __PInvoke(&__boolStruct_native, numValues); + } + + // Unmarshal - Convert native data to managed data. + __boolStruct_native__numElements = numValues; + boolStruct = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__boolStruct_native, __boolStruct_native__numElements); + { + System.ReadOnlySpan __boolStruct_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__boolStruct_native, __boolStruct_native__numElements); + System.Span __boolStruct_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(boolStruct); + for (int __i0 = 0; __i0 < __boolStruct_native__numElements; ++__i0) + { + __boolStruct_native__managedSpan[__i0] = global::LibraryImportGenerator.IntegrationTests.EnforceLastElementMarshalledCleanupBoolStruct.ConvertToManaged(__boolStruct_native__nativeSpan[__i0]); + } + } + } + finally + { + // Cleanup - Perform required cleanup. + __boolStruct_native__numElements = numValues; + { + System.ReadOnlySpan __boolStruct_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__boolStruct_native, __boolStruct_native__numElements); + for (int __i0 = 0; __i0 < __boolStruct_native__lastIndexMarshalled; ++__i0) + { + global::LibraryImportGenerator.IntegrationTests.EnforceLastElementMarshalledCleanupBoolStruct.Free(__boolStruct_native__nativeSpan[__i0]); + } + } + + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__boolStruct_native); + } + + // Local P/Invoke + [System.Runtime.InteropServices.DllImportAttribute("Microsoft.Interop.Tests.NativeExportsNE", EntryPoint = "negate_bool_struct_array_ref", ExactSpelling = true)] + static extern unsafe void __PInvoke(nint** __boolStruct_native, int __numValues_native); + } } } } @@ -2001,35 +2887,168 @@ namespace LibraryImportGenerator.IntegrationTests { unsafe partial class NativeExportsNE { - [System.Runtime.InteropServices.DllImportAttribute("Microsoft.Interop.Tests.NativeExportsNE", EntryPoint = "char_return_as_uint", ExactSpelling = true)] - public static extern partial char ReturnU2AsU2IgnoreCharSet(char input); - } -} -namespace LibraryImportGenerator.IntegrationTests -{ - unsafe partial class NativeExportsNE - { - [System.Runtime.InteropServices.DllImportAttribute("Microsoft.Interop.Tests.NativeExportsNE", EntryPoint = "char_return_as_uint", ExactSpelling = true)] - public static extern partial char ReturnI2AsI2IgnoreCharSet(char input); + public unsafe partial class MarshallingFails + { + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "42.42.42.42")] + [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] + public static partial void NegateBoolsRef2D_LastElementMarshalling(ref global::SharedTypes.BoolStruct[][] boolStruct, int length) + { + nint* __boolStruct_native = default; + // Setup - Perform required setup. + int __boolStruct_native__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __boolStruct_native__numElements); + int __boolStruct_native__lastIndexMarshalled = 0; + try + { + // Marshal - Convert managed data to native data. + __boolStruct_native = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(boolStruct, out __boolStruct_native__numElements); + { + System.ReadOnlySpan __boolStruct_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(boolStruct); + System.Span __boolStruct_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__boolStruct_native, __boolStruct_native__numElements); + for (int __i0 = 0; __i0 < __boolStruct_native__managedSpan.Length; ++__i0, ++__boolStruct_native__lastIndexMarshalled) + { + __boolStruct_native__nativeSpan[__i0] = global::LibraryImportGenerator.IntegrationTests.EnforceLastElementMarshalledCleanupBoolStructArray.ConvertToUnmanaged(__boolStruct_native__managedSpan[__i0]); + } + } + + { + __PInvoke(&__boolStruct_native, length); + } + + // Unmarshal - Convert native data to managed data. + __boolStruct_native__numElements = length; + boolStruct = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__boolStruct_native, __boolStruct_native__numElements); + { + System.ReadOnlySpan __boolStruct_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__boolStruct_native, __boolStruct_native__numElements); + System.Span __boolStruct_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(boolStruct); + for (int __i0 = 0; __i0 < __boolStruct_native__numElements; ++__i0) + { + __boolStruct_native__managedSpan[__i0] = global::LibraryImportGenerator.IntegrationTests.EnforceLastElementMarshalledCleanupBoolStructArray.ConvertToManaged(__boolStruct_native__nativeSpan[__i0]); + } + } + } + finally + { + // Cleanup - Perform required cleanup. + __boolStruct_native__numElements = length; + { + System.ReadOnlySpan __boolStruct_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__boolStruct_native, __boolStruct_native__numElements); + for (int __i0 = 0; __i0 < __boolStruct_native__lastIndexMarshalled; ++__i0) + { + global::LibraryImportGenerator.IntegrationTests.EnforceLastElementMarshalledCleanupBoolStructArray.Free(__boolStruct_native__nativeSpan[__i0]); + } + } + + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__boolStruct_native); + } + + // Local P/Invoke + [System.Runtime.InteropServices.DllImportAttribute("DoesNotExist", EntryPoint = "negate_bool_struct_array_ref_2d", ExactSpelling = true)] + static extern unsafe void __PInvoke(nint** __boolStruct_native, int __length_native); + } + } } } namespace LibraryImportGenerator.IntegrationTests { unsafe partial class NativeExportsNE { - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "42.42.42.42")] - [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] - public static partial void ReverseBuffer(ref char buffer, int len) + public unsafe partial class MarshallingFails { - // Pin - Pin data in preparation for calling the P/Invoke. - fixed (char* __buffer_native = &buffer) + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "42.42.42.42")] + [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] + public static partial void NegateBoolsRef2D_ClearMarshalling(ref global::SharedTypes.BoolStruct[][] boolStruct, int length, int[] widths) { - __PInvoke(__buffer_native, len); - } + System.IntPtr* __boolStruct_native = default; + // Setup - Perform required setup. + int __boolStruct_native__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __boolStruct_native__numElements); + int __boolStruct_native__lastIndexMarshalled = 0; + try + { + // Marshal - Convert managed data to native data. + __boolStruct_native = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(boolStruct, out __boolStruct_native__numElements); + { + System.ReadOnlySpan __boolStruct_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(boolStruct); + System.Span __boolStruct_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__boolStruct_native, __boolStruct_native__numElements); + for (int __i0 = 0; __i0 < __boolStruct_native__managedSpan.Length; ++__i0, ++__boolStruct_native__lastIndexMarshalled) + { + int __boolStruct_native__nativeSpan____i0__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __boolStruct_native__nativeSpan____i0__numElements); + ; + __boolStruct_native__nativeSpan[__i0] = (System.IntPtr)global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(__boolStruct_native__managedSpan[__i0], out __boolStruct_native__nativeSpan____i0__numElements); + { + System.ReadOnlySpan __boolStruct_native__nativeSpan____i0__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(__boolStruct_native__managedSpan[__i0]); + System.Span __boolStruct_native__nativeSpan____i0__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination((nint*)__boolStruct_native__nativeSpan[__i0], __boolStruct_native__nativeSpan____i0__numElements); + for (int __i1 = 0; __i1 < __boolStruct_native__nativeSpan____i0__managedSpan.Length; ++__i1) + { + __boolStruct_native__nativeSpan____i0__nativeSpan[__i1] = global::LibraryImportGenerator.IntegrationTests.EnforceClearedMemoryCleanup.ConvertToUnmanaged(__boolStruct_native__nativeSpan____i0__managedSpan[__i1]); + } + } + } + } - // Local P/Invoke - [System.Runtime.InteropServices.DllImportAttribute("Microsoft.Interop.Tests.NativeExportsNE", EntryPoint = "char_reverse_buffer_ref", ExactSpelling = true)] - static extern unsafe void __PInvoke(char* __buffer_native, int __len_native); + // Pin - Pin data in preparation for calling the P/Invoke. + fixed (void* __widths_native = &global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn.GetPinnableReference(widths)) + { + __PInvoke(&__boolStruct_native, length, (int*)__widths_native); + } + + // Unmarshal - Convert native data to managed data. + __boolStruct_native__numElements = length; + boolStruct = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__boolStruct_native, __boolStruct_native__numElements); + { + System.ReadOnlySpan __boolStruct_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__boolStruct_native, __boolStruct_native__numElements); + System.Span __boolStruct_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(boolStruct); + for (int __i0 = 0; __i0 < __boolStruct_native__numElements; ++__i0) + { + int __boolStruct_native__nativeSpan____i0__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __boolStruct_native__nativeSpan____i0__numElements); + ; + __boolStruct_native__nativeSpan____i0__numElements = widths[__i0]; + __boolStruct_native__managedSpan[__i0] = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements((nint*)__boolStruct_native__nativeSpan[__i0], __boolStruct_native__nativeSpan____i0__numElements); + { + System.ReadOnlySpan __boolStruct_native__nativeSpan____i0__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource((nint*)__boolStruct_native__nativeSpan[__i0], __boolStruct_native__nativeSpan____i0__numElements); + System.Span __boolStruct_native__nativeSpan____i0__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(__boolStruct_native__managedSpan[__i0]); + for (int __i1 = 0; __i1 < __boolStruct_native__nativeSpan____i0__numElements; ++__i1) + { + __boolStruct_native__nativeSpan____i0__managedSpan[__i1] = global::LibraryImportGenerator.IntegrationTests.EnforceClearedMemoryCleanup.ConvertToManaged(__boolStruct_native__nativeSpan____i0__nativeSpan[__i1]); + } + } + } + } + } + finally + { + // Cleanup - Perform required cleanup. + __boolStruct_native__numElements = length; + { + System.ReadOnlySpan __boolStruct_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__boolStruct_native, __boolStruct_native__numElements); + for (int __i0 = 0; __i0 < __boolStruct_native__lastIndexMarshalled; ++__i0) + { + int __boolStruct_native__nativeSpan____i0__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __boolStruct_native__nativeSpan____i0__numElements); + ; + __boolStruct_native__nativeSpan____i0__numElements = widths[__i0]; + { + System.ReadOnlySpan __boolStruct_native__nativeSpan____i0__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination((nint*)__boolStruct_native__nativeSpan[__i0], __boolStruct_native__nativeSpan____i0__numElements); + for (int __i1 = 0; __i1 < __boolStruct_native__nativeSpan____i0__nativeSpan.Length; ++__i1) + { + global::LibraryImportGenerator.IntegrationTests.EnforceClearedMemoryCleanup.Free(__boolStruct_native__nativeSpan____i0__nativeSpan[__i1]); + } + } + + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__boolStruct_native__nativeSpan[__i0]); + } + } + + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__boolStruct_native); + } + + // Local P/Invoke + [System.Runtime.InteropServices.DllImportAttribute("DoesNotExist", EntryPoint = "negate_bool_struct_array_ref_2d", ExactSpelling = true)] + static extern unsafe void __PInvoke(System.IntPtr** __boolStruct_native, int __length_native, int* __widths_native); + } } } } @@ -2046,10 +3065,11 @@ public unsafe partial class Stateless public static partial int Sum(global::System.Collections.Generic.List values, int numValues) { byte* __values_native = default; - int __retVal; + int __retVal = default; // Setup - Perform required setup. int __values_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); + ; try { // Marshal - Convert managed data to native data. @@ -2062,6 +3082,7 @@ public static partial int Sum(global::System.Collections.Generic.List value finally { // Cleanup - Perform required cleanup. + ; global::SharedTypes.ListMarshaller.Free(__values_native); } @@ -2086,17 +3107,26 @@ public unsafe partial class Stateless [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial int SumWithBuffer(global::System.Collections.Generic.List values, int numValues) { - byte* __values_native; - int __retVal; + byte* __values_native = default; + int __retVal = default; // Setup - Perform required setup. int __values_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); - // Marshal - Convert managed data to native data. - System.Span __values_native__buffer = stackalloc byte[global::SharedTypes.ListMarshallerWithBuffer.BufferSize]; - __values_native = global::SharedTypes.ListMarshallerWithBuffer.AllocateContainerForUnmanagedElements(values, __values_native__buffer, out __values_native__numElements); - global::SharedTypes.ListMarshallerWithBuffer.GetManagedValuesSource(values).CopyTo(global::SharedTypes.ListMarshallerWithBuffer.GetUnmanagedValuesDestination(__values_native, __values_native__numElements)); + ; + try + { + // Marshal - Convert managed data to native data. + System.Span __values_native__buffer = stackalloc byte[global::SharedTypes.ListMarshallerWithBuffer.BufferSize]; + __values_native = global::SharedTypes.ListMarshallerWithBuffer.AllocateContainerForUnmanagedElements(values, __values_native__buffer, out __values_native__numElements); + global::SharedTypes.ListMarshallerWithBuffer.GetManagedValuesSource(values).CopyTo(global::SharedTypes.ListMarshallerWithBuffer.GetUnmanagedValuesDestination(__values_native, __values_native__numElements)); + { + __retVal = __PInvoke(__values_native, numValues); + } + } + finally { - __retVal = __PInvoke(__values_native, numValues); + // Cleanup - Perform required cleanup. + ; } return __retVal; @@ -2121,10 +3151,11 @@ public unsafe partial class Stateless public static unsafe partial int SumWithFreeTracking(global::System.Collections.Generic.List values, int numValues) { byte* __values_native = default; - int __retVal; + int __retVal = default; // Setup - Perform required setup. int __values_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); + int __values_native__lastIndexMarshalled = 0; try { // Marshal - Convert managed data to native data. @@ -2132,7 +3163,7 @@ public static unsafe partial int SumWithFreeTracking(global::System.Collections. { System.ReadOnlySpan __values_native__managedSpan = global::SharedTypes.ListMarshaller.GetManagedValuesSource(values); System.Span __values_native__nativeSpan = global::SharedTypes.ListMarshaller.GetUnmanagedValuesDestination(__values_native, __values_native__numElements); - for (int __i0 = 0; __i0 < __values_native__managedSpan.Length; ++__i0) + for (int __i0 = 0; __i0 < __values_native__managedSpan.Length; ++__i0, ++__values_native__lastIndexMarshalled) { __values_native__nativeSpan[__i0] = (System.IntPtr)(int*)global::SharedTypes.IntWrapperMarshallerWithFreeCounts.ConvertToUnmanaged(__values_native__managedSpan[__i0]); } @@ -2145,9 +3176,10 @@ public static unsafe partial int SumWithFreeTracking(global::System.Collections. finally { // Cleanup - Perform required cleanup. + ; { System.ReadOnlySpan __values_native__nativeSpan = global::SharedTypes.ListMarshaller.GetUnmanagedValuesDestination(__values_native, __values_native__numElements); - for (int __i0 = 0; __i0 < __values_native__nativeSpan.Length; ++__i0) + for (int __i0 = 0; __i0 < __values_native__lastIndexMarshalled; ++__i0) { global::SharedTypes.IntWrapperMarshallerWithFreeCounts.Free((int*)__values_native__nativeSpan[__i0]); } @@ -2206,10 +3238,11 @@ public unsafe partial class Stateless public static partial int SumInArray(in global::System.Collections.Generic.List values, int numValues) { byte* __values_native = default; - int __retVal; + int __retVal = default; // Setup - Perform required setup. int __values_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); + ; try { // Marshal - Convert managed data to native data. @@ -2222,6 +3255,7 @@ public static partial int SumInArray(in global::System.Collections.Generic.List< finally { // Cleanup - Perform required cleanup. + ; global::SharedTypes.ListMarshaller.Free(__values_native); } @@ -2250,6 +3284,7 @@ public static partial void Duplicate(ref global::System.Collections.Generic.List // Setup - Perform required setup. int __values_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); + ; try { // Marshal - Convert managed data to native data. @@ -2267,6 +3302,7 @@ public static partial void Duplicate(ref global::System.Collections.Generic.List finally { // Cleanup - Perform required cleanup. + __values_native__numElements = numValues; global::SharedTypes.ListMarshaller.Free(__values_native); } @@ -2291,11 +3327,12 @@ public unsafe partial class Stateless public static partial global::System.Collections.Generic.List CreateRange(int start, int end, out int numValues) { System.Runtime.CompilerServices.Unsafe.SkipInit(out numValues); - global::System.Collections.Generic.List __retVal; + global::System.Collections.Generic.List __retVal = default; byte* __retVal_native = default; // Setup - Perform required setup. int __retVal_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__numElements); + ; try { // Pin - Pin data in preparation for calling the P/Invoke. @@ -2312,6 +3349,7 @@ public unsafe partial class Stateless finally { // Cleanup - Perform required cleanup. + __retVal_native__numElements = numValues; global::SharedTypes.ListMarshaller.Free(__retVal_native); } @@ -2342,6 +3380,7 @@ public static partial void CreateRange_Out(int start, int end, out int numValues // Setup - Perform required setup. int __res_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __res_native__numElements); + ; try { // Pin - Pin data in preparation for calling the P/Invoke. @@ -2358,6 +3397,7 @@ public static partial void CreateRange_Out(int start, int end, out int numValues finally { // Cleanup - Perform required cleanup. + __res_native__numElements = numValues; global::SharedTypes.ListMarshaller.Free(__res_native); } @@ -2381,11 +3421,12 @@ public unsafe partial class Stateless [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial global::System.Collections.Generic.List GetLongBytes(long l) { - global::System.Collections.Generic.List __retVal; + global::System.Collections.Generic.List __retVal = default; byte* __retVal_native = default; // Setup - Perform required setup. int __retVal_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__numElements); + ; try { { @@ -2400,6 +3441,7 @@ public unsafe partial class Stateless finally { // Cleanup - Perform required cleanup. + __retVal_native__numElements = 8; global::SharedTypes.ListMarshaller.Free(__retVal_native); } @@ -2425,11 +3467,12 @@ public unsafe partial class Stateless public static partial bool AndAllMembers(global::System.Collections.Generic.List pArray, int length) { byte* __pArray_native = default; - bool __retVal; + bool __retVal = default; byte __retVal_native = default; // Setup - Perform required setup. int __pArray_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __pArray_native__numElements); + ; try { // Marshal - Convert managed data to native data. @@ -2453,6 +3496,7 @@ public static partial bool AndAllMembers(global::System.Collections.Generic.List finally { // Cleanup - Perform required cleanup. + ; global::SharedTypes.ListMarshaller.Free(__pArray_native); } @@ -2478,11 +3522,12 @@ public unsafe partial class Stateless public static partial bool AndAllMembersIn(in global::System.Collections.Generic.List pArray, int length) { byte* __pArray_native = default; - bool __retVal; + bool __retVal = default; byte __retVal_native = default; // Setup - Perform required setup. int __pArray_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __pArray_native__numElements); + ; try { // Marshal - Convert managed data to native data. @@ -2506,6 +3551,7 @@ public static partial bool AndAllMembersIn(in global::System.Collections.Generic finally { // Cleanup - Perform required cleanup. + ; global::SharedTypes.ListMarshaller.Free(__pArray_native); } @@ -2534,6 +3580,7 @@ public static partial void NegateBools(ref global::System.Collections.Generic.Li // Setup - Perform required setup. int __boolStruct_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __boolStruct_native__numElements); + ; try { // Marshal - Convert managed data to native data. @@ -2566,6 +3613,7 @@ public static partial void NegateBools(ref global::System.Collections.Generic.Li finally { // Cleanup - Perform required cleanup. + __boolStruct_native__numElements = numValues; global::SharedTypes.ListMarshaller.Free(__boolStruct_native); } @@ -2595,8 +3643,10 @@ public static partial void NegateBools(global::System.Collections.Generic.List.Free(__pBoolStructOut_native); + ; global::SharedTypes.ListMarshaller.Free(__boolStruct_native); } @@ -2654,13 +3706,15 @@ public unsafe partial class Stateless public static partial global::System.Collections.Generic.List NegateBools(global::System.Collections.Generic.List boolStruct, int numValues) { byte* __boolStruct_native = default; - global::System.Collections.Generic.List __retVal; + global::System.Collections.Generic.List __retVal = default; byte* __retVal_native = default; // Setup - Perform required setup. int __retVal_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__numElements); + ; int __boolStruct_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __boolStruct_native__numElements); + ; try { // Marshal - Convert managed data to native data. @@ -2693,7 +3747,9 @@ public unsafe partial class Stateless finally { // Cleanup - Perform required cleanup. + __retVal_native__numElements = numValues; global::SharedTypes.ListMarshaller.Free(__retVal_native); + ; global::SharedTypes.ListMarshaller.Free(__boolStruct_native); } @@ -2721,11 +3777,12 @@ public static partial int GuaranteedUnmarshal(out global::System.Collections.Gen bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out ret); byte* __ret_native = default; - int __retVal; + int __retVal = default; int __retVal_native = default; // Setup - Perform required setup. int __ret_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __ret_native__numElements); + ; try { { @@ -2744,6 +3801,9 @@ public static partial int GuaranteedUnmarshal(out global::System.Collections.Gen __ret_native__numElements = 1; ret = global::LibraryImportGenerator.IntegrationTests.NativeExportsNE.Collections.Stateless.ListGuaranteedUnmarshal.AllocateContainerForManagedElementsFinally(__ret_native, __ret_native__numElements); } + + // Cleanup - Perform required cleanup. + __ret_native__numElements = 1; } return __retVal; @@ -2770,11 +3830,12 @@ public static partial int GuaranteedUnmarshal(out global::System.Collections.Gen bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out ret); byte* __ret_native = default; - int __retVal; + int __retVal = default; int __retVal_native = default; // Setup - Perform required setup. int __ret_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __ret_native__numElements); + ; try { { @@ -2793,6 +3854,9 @@ public static partial int GuaranteedUnmarshal(out global::System.Collections.Gen __ret_native__numElements = 1; ret = global::LibraryImportGenerator.IntegrationTests.NativeExportsNE.Collections.Stateless.ListGuaranteedUnmarshal.AllocateContainerForManagedElementsFinally(__ret_native, __ret_native__numElements); } + + // Cleanup - Perform required cleanup. + __ret_native__numElements = 1; } return __retVal; @@ -2817,10 +3881,11 @@ public unsafe partial class Stateful public static partial int Sum(global::System.Collections.Generic.List values, int numValues) { byte* __values_native = default; - int __retVal; + int __retVal = default; // Setup - Perform required setup. scoped global::SharedTypes.ListMarshallerStateful.Marshaller __values_native__marshaller = new(); int __values_native__numElements; + ; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); try { @@ -2863,10 +3928,11 @@ public unsafe partial class Stateful public static unsafe partial int SumWithFreeTracking(global::System.Collections.Generic.List values, int numValues) { byte* __values_native = default; - int __retVal; + int __retVal = default; // Setup - Perform required setup. scoped global::SharedTypes.ListMarshallerStateful.Marshaller __values_native__marshaller = new(); int __values_native__numElements; + int __values_native__lastIndexMarshalled = 0; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); try { @@ -2875,7 +3941,7 @@ public static unsafe partial int SumWithFreeTracking(global::System.Collections. { System.ReadOnlySpan __values_native__managedSpan = __values_native__marshaller.GetManagedValuesSource(); System.Span __values_native__nativeSpan = __values_native__marshaller.GetUnmanagedValuesDestination(); - for (int __i0 = 0; __i0 < __values_native__managedSpan.Length; ++__i0) + for (int __i0 = 0; __i0 < __values_native__managedSpan.Length; ++__i0, ++__values_native__lastIndexMarshalled) { __values_native__nativeSpan[__i0] = (System.IntPtr)(int*)global::SharedTypes.IntWrapperMarshallerWithFreeCounts.ConvertToUnmanaged(__values_native__managedSpan[__i0]); } @@ -2894,7 +3960,7 @@ public static unsafe partial int SumWithFreeTracking(global::System.Collections. // Cleanup - Perform required cleanup. { System.ReadOnlySpan __values_native__nativeSpan = __values_native__marshaller.GetUnmanagedValuesDestination(); - for (int __i0 = 0; __i0 < __values_native__nativeSpan.Length; ++__i0) + for (int __i0 = 0; __i0 < __values_native__lastIndexMarshalled; ++__i0) { global::SharedTypes.IntWrapperMarshallerWithFreeCounts.Free((int*)__values_native__nativeSpan[__i0]); } @@ -2925,10 +3991,11 @@ public unsafe partial class Stateful public static partial int SumInArray(in global::System.Collections.Generic.List values, int numValues) { byte* __values_native = default; - int __retVal; + int __retVal = default; // Setup - Perform required setup. scoped global::SharedTypes.ListMarshallerStateful.Marshaller __values_native__marshaller = new(); int __values_native__numElements; + ; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); try { @@ -2974,6 +4041,7 @@ public static partial void Duplicate(ref global::System.Collections.Generic.List // Setup - Perform required setup. scoped global::SharedTypes.ListMarshallerStateful.Marshaller __values_native__marshaller = new(); int __values_native__numElements; + ; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); try { @@ -3020,11 +4088,12 @@ public unsafe partial class Stateful public static partial global::System.Collections.Generic.List CreateRange(int start, int end, out int numValues) { System.Runtime.CompilerServices.Unsafe.SkipInit(out numValues); - global::System.Collections.Generic.List __retVal; + global::System.Collections.Generic.List __retVal = default; byte* __retVal_native = default; // Setup - Perform required setup. scoped global::SharedTypes.ListMarshallerStateful.Marshaller __retVal_native__marshaller = new(); int __retVal_native__numElements; + ; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__numElements); try { @@ -3074,6 +4143,7 @@ public static partial void CreateRange_Out(int start, int end, out int numValues // Setup - Perform required setup. scoped global::SharedTypes.ListMarshallerStateful.Marshaller __res_native__marshaller = new(); int __res_native__numElements; + ; System.Runtime.CompilerServices.Unsafe.SkipInit(out __res_native__numElements); try { @@ -3116,11 +4186,12 @@ public unsafe partial class Stateful [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial global::System.Collections.Generic.List GetLongBytes(long l) { - global::System.Collections.Generic.List __retVal; + global::System.Collections.Generic.List __retVal = default; byte* __retVal_native = default; // Setup - Perform required setup. scoped global::SharedTypes.ListMarshallerStateful.Marshaller __retVal_native__marshaller = new(); int __retVal_native__numElements; + ; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__numElements); try { @@ -3163,11 +4234,12 @@ public unsafe partial class Stateful public static partial bool AndAllMembers(global::System.Collections.Generic.List pArray, int length) { byte* __pArray_native = default; - bool __retVal; + bool __retVal = default; byte __retVal_native = default; // Setup - Perform required setup. scoped global::SharedTypes.ListMarshallerStateful.Marshaller __pArray_native__marshaller = new(); int __pArray_native__numElements; + ; System.Runtime.CompilerServices.Unsafe.SkipInit(out __pArray_native__numElements); try { @@ -3221,11 +4293,12 @@ public unsafe partial class Stateful public static partial bool AndAllMembersIn(in global::System.Collections.Generic.List pArray, int length) { byte* __pArray_native = default; - bool __retVal; + bool __retVal = default; byte __retVal_native = default; // Setup - Perform required setup. scoped global::SharedTypes.ListMarshallerStateful.Marshaller __pArray_native__marshaller = new(); int __pArray_native__numElements; + ; System.Runtime.CompilerServices.Unsafe.SkipInit(out __pArray_native__numElements); try { @@ -3282,6 +4355,7 @@ public static partial void NegateBools(ref global::System.Collections.Generic.Li // Setup - Perform required setup. scoped global::SharedTypes.ListMarshallerStateful.Marshaller __boolStruct_native__marshaller = new(); int __boolStruct_native__numElements; + ; System.Runtime.CompilerServices.Unsafe.SkipInit(out __boolStruct_native__numElements); try { @@ -3349,9 +4423,11 @@ public static partial void NegateBools(global::System.Collections.Generic.List.Marshaller __pBoolStructOut_native__marshaller = new(); int __pBoolStructOut_native__numElements; + ; System.Runtime.CompilerServices.Unsafe.SkipInit(out __pBoolStructOut_native__numElements); scoped global::SharedTypes.ListMarshallerStateful.Marshaller __boolStruct_native__marshaller = new(); int __boolStruct_native__numElements; + ; System.Runtime.CompilerServices.Unsafe.SkipInit(out __boolStruct_native__numElements); try { @@ -3417,14 +4493,16 @@ public unsafe partial class Stateful public static partial global::System.Collections.Generic.List NegateBools(global::System.Collections.Generic.List boolStruct, int numValues) { byte* __boolStruct_native = default; - global::System.Collections.Generic.List __retVal; + global::System.Collections.Generic.List __retVal = default; byte* __retVal_native = default; // Setup - Perform required setup. scoped global::SharedTypes.ListMarshallerStateful.Marshaller __retVal_native__marshaller = new(); int __retVal_native__numElements; + ; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__numElements); scoped global::SharedTypes.ListMarshallerStateful.Marshaller __boolStruct_native__marshaller = new(); int __boolStruct_native__numElements; + ; System.Runtime.CompilerServices.Unsafe.SkipInit(out __boolStruct_native__numElements); try { @@ -3493,11 +4571,12 @@ public static partial int GuaranteedUnmarshal(out global::System.Collections.Gen bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out ret); byte* __ret_native = default; - int __retVal; + int __retVal = default; int __retVal_native = default; // Setup - Perform required setup. global::LibraryImportGenerator.IntegrationTests.NativeExportsNE.Collections.Stateful.ListGuaranteedUnmarshal.Marshaller __ret_native__marshaller = new(); int __ret_native__numElements; + ; System.Runtime.CompilerServices.Unsafe.SkipInit(out __ret_native__numElements); try { @@ -3547,11 +4626,12 @@ public static partial int GuaranteedUnmarshal(out global::System.Collections.Gen bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out ret); byte* __ret_native = default; - int __retVal; + int __retVal = default; int __retVal_native = default; // Setup - Perform required setup. global::LibraryImportGenerator.IntegrationTests.NativeExportsNE.Collections.Stateful.ListGuaranteedUnmarshal.Marshaller __ret_native__marshaller = new(); int __ret_native__numElements; + ; System.Runtime.CompilerServices.Unsafe.SkipInit(out __ret_native__numElements); try { @@ -3780,7 +4860,7 @@ internal unsafe partial class Stateless public static partial global::SharedTypes.IntWrapper DoubleIntRef(global::SharedTypes.IntWrapper pInt) { int* __pInt_native = default; - global::SharedTypes.IntWrapper __retVal; + global::SharedTypes.IntWrapper __retVal = default; int* __retVal_native = default; try { @@ -3818,7 +4898,7 @@ internal unsafe partial class Stateless [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial global::SharedTypes.IntWrapperWithoutGetPinnableReference DoubleIntRef(global::SharedTypes.IntWrapperWithoutGetPinnableReference pInt) { - global::SharedTypes.IntWrapperWithoutGetPinnableReference __retVal; + global::SharedTypes.IntWrapperWithoutGetPinnableReference __retVal = default; int* __retVal_native = default; try { @@ -3858,7 +4938,7 @@ public static partial int GuaranteedUnmarshal(out int ret) bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out ret); int __ret_native = default; - int __retVal; + int __retVal = default; int __retVal_native = default; try { @@ -4050,7 +5130,7 @@ internal unsafe partial class Stateful [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial global::SharedTypes.IntWrapperWithoutGetPinnableReference DoubleIntRef(global::SharedTypes.IntWrapperWithoutGetPinnableReference pInt) { - global::SharedTypes.IntWrapperWithoutGetPinnableReference __retVal; + global::SharedTypes.IntWrapperWithoutGetPinnableReference __retVal = default; int* __retVal_native = default; try { @@ -4088,7 +5168,7 @@ internal unsafe partial class Stateful public static partial global::SharedTypes.IntWrapperWithoutGetPinnableReference DoubleIntRefNoAlloc(global::SharedTypes.IntWrapperWithoutGetPinnableReference pInt) { int* __pInt_native = default; - global::SharedTypes.IntWrapperWithoutGetPinnableReference __retVal; + global::SharedTypes.IntWrapperWithoutGetPinnableReference __retVal = default; int* __retVal_native = default; // Setup - Perform required setup. global::SharedTypes.IntWrapperWithoutGetPinnableReferenceStatefulNoAllocMarshaller.StatefulGetPinnableReference __pInt_native__marshaller = new(); @@ -4132,7 +5212,7 @@ internal unsafe partial class Stateful public static partial global::SharedTypes.IntWrapper DoubleIntRef(global::SharedTypes.IntWrapper pInt) { int* __pInt_native = default; - global::SharedTypes.IntWrapper __retVal; + global::SharedTypes.IntWrapper __retVal = default; int* __retVal_native = default; // Setup - Perform required setup. global::SharedTypes.IntWrapperMarshallerStateful.Marshaller __retVal_native__marshaller = new(); @@ -4180,7 +5260,7 @@ public static partial int GuaranteedUnmarshal(out int ret) bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out ret); int __ret_native = default; - int __retVal; + int __retVal = default; int __retVal_native = default; // Setup - Perform required setup. global::LibraryImportGenerator.IntegrationTests.NativeExportsNE.Stateful.IntGuaranteedUnmarshal.Marshaller __retVal_native__marshaller = new(); @@ -4580,7 +5660,7 @@ public unsafe partial class NativeExportsSafeHandle [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] private static partial global::LibraryImportGenerator.IntegrationTests.NativeExportsNE.NativeExportsSafeHandle AllocateHandle() { - global::LibraryImportGenerator.IntegrationTests.NativeExportsNE.NativeExportsSafeHandle __retVal; + global::LibraryImportGenerator.IntegrationTests.NativeExportsNE.NativeExportsSafeHandle __retVal = default; nint __retVal_native = default; // Setup - Perform required setup. global::System.Runtime.InteropServices.Marshalling.SafeHandleMarshaller.ManagedToUnmanagedOut __retVal_native__marshaller = new(); @@ -4617,7 +5697,7 @@ unsafe partial class NativeExportsNE [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial global::LibraryImportGenerator.IntegrationTests.NativeExportsNE.NativeExportsSafeHandle AllocateHandle() { - global::LibraryImportGenerator.IntegrationTests.NativeExportsNE.NativeExportsSafeHandle __retVal; + global::LibraryImportGenerator.IntegrationTests.NativeExportsNE.NativeExportsSafeHandle __retVal = default; nint __retVal_native = default; // Setup - Perform required setup. global::System.Runtime.InteropServices.Marshalling.SafeHandleMarshaller.ManagedToUnmanagedOut __retVal_native__marshaller = new(); @@ -4712,7 +5792,7 @@ unsafe partial class NativeExportsNE public static partial bool IsHandleAlive(global::LibraryImportGenerator.IntegrationTests.NativeExportsNE.NativeExportsSafeHandle handle) { nint __handle_native = default; - bool __retVal; + bool __retVal = default; sbyte __retVal_native = default; // Setup - Perform required setup. global::System.Runtime.InteropServices.Marshalling.SafeHandleMarshaller.ManagedToUnmanagedIn __handle_native__marshaller = new(); @@ -4859,7 +5939,7 @@ public static partial string SetError_NonBlittableSignature(int error, bool shou { int __lastError; byte __shouldSetError_native = default; - string __retVal; + string __retVal = default; ushort* __retVal_native = default; try { @@ -4952,10 +6032,11 @@ public unsafe partial class Span public static partial int SumInArray(in global::System.Span values, int numValues) { int* __values_native = default; - int __retVal; + int __retVal = default; // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.SpanMarshaller.ManagedToUnmanagedIn __values_native__marshaller = new(); int __values_native__numElements; + ; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); try { @@ -4998,6 +6079,7 @@ public static partial void Duplicate(ref global::System.Span values, int nu // Setup - Perform required setup. int __values_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); + ; try { // Marshal - Convert managed data to native data. @@ -5015,6 +6097,7 @@ public static partial void Duplicate(ref global::System.Span values, int nu finally { // Cleanup - Perform required cleanup. + __values_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.SpanMarshaller.Free(__values_native); } @@ -5036,11 +6119,12 @@ public unsafe partial class Span public static partial global::System.Span CreateRange(int start, int end, out int numValues) { System.Runtime.CompilerServices.Unsafe.SkipInit(out numValues); - global::System.Span __retVal; + global::System.Span __retVal = default; int* __retVal_native = default; // Setup - Perform required setup. int __retVal_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__numElements); + ; try { // Pin - Pin data in preparation for calling the P/Invoke. @@ -5057,6 +6141,7 @@ public unsafe partial class Span finally { // Cleanup - Perform required cleanup. + __retVal_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.SpanMarshaller.Free(__retVal_native); } @@ -5084,6 +6169,7 @@ public static partial void CreateRange_Out(int start, int end, out int numValues // Setup - Perform required setup. int __res_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __res_native__numElements); + ; try { // Pin - Pin data in preparation for calling the P/Invoke. @@ -5100,6 +6186,7 @@ public static partial void CreateRange_Out(int start, int end, out int numValues finally { // Cleanup - Perform required cleanup. + __res_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.SpanMarshaller.Free(__res_native); } @@ -5120,11 +6207,12 @@ public unsafe partial class Span [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial global::System.Span GetLongBytes(long l) { - global::System.Span __retVal; + global::System.Span __retVal = default; byte* __retVal_native = default; // Setup - Perform required setup. int __retVal_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__numElements); + ; try { { @@ -5139,6 +6227,7 @@ public unsafe partial class Span finally { // Cleanup - Perform required cleanup. + __retVal_native__numElements = 8; global::System.Runtime.InteropServices.Marshalling.SpanMarshaller.Free(__retVal_native); } @@ -5161,11 +6250,12 @@ public unsafe partial class Span public static partial bool AndAllMembers(global::System.Span pArray, int length) { global::SharedTypes.BoolStructMarshaller.BoolStructNative* __pArray_native = default; - bool __retVal; + bool __retVal = default; byte __retVal_native = default; // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.SpanMarshaller.ManagedToUnmanagedIn __pArray_native__marshaller = new(); int __pArray_native__numElements; + ; System.Runtime.CompilerServices.Unsafe.SkipInit(out __pArray_native__numElements); try { @@ -5240,7 +6330,7 @@ public unsafe partial class Utf16 [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial string Reverse_Return(string s) { - string __retVal; + string __retVal = default; ushort* __retVal_native = default; try { @@ -5465,7 +6555,7 @@ public unsafe partial class LPTStr [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial string Reverse_Return(string s) { - string __retVal; + string __retVal = default; ushort* __retVal_native = default; try { @@ -5690,7 +6780,7 @@ public unsafe partial class LPWStr [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial string Reverse_Return(string s) { - string __retVal; + string __retVal = default; ushort* __retVal_native = default; try { @@ -5866,7 +6956,7 @@ public unsafe partial class LPUTF8Str public static partial int ReturnLength(string s) { byte* __s_native = default; - int __retVal; + int __retVal = default; // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn __s_native__marshaller = new(); try @@ -5904,7 +6994,7 @@ public unsafe partial class LPUTF8Str public static partial int ReturnLength_IgnoreStringMarshalling(string s) { byte* __s_native = default; - int __retVal; + int __retVal = default; // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn __s_native__marshaller = new(); try @@ -5942,7 +7032,7 @@ public unsafe partial class LPUTF8Str public static partial string Reverse_Return(string s) { byte* __s_native = default; - string __retVal; + string __retVal = default; byte* __retVal_native = default; // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn __s_native__marshaller = new(); @@ -6133,7 +7223,7 @@ public unsafe partial class Utf8 public static partial int ReturnLength(string s) { byte* __s_native = default; - int __retVal; + int __retVal = default; // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn __s_native__marshaller = new(); try @@ -6171,7 +7261,7 @@ public unsafe partial class Utf8 public static partial string Reverse_Return(string s) { byte* __s_native = default; - string __retVal; + string __retVal = default; byte* __retVal_native = default; // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn __s_native__marshaller = new(); @@ -6362,7 +7452,7 @@ public unsafe partial class LPStr public static partial int ReturnLength(string s) { byte* __s_native = default; - int __retVal; + int __retVal = default; // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.AnsiStringMarshaller.ManagedToUnmanagedIn __s_native__marshaller = new(); try @@ -6400,7 +7490,7 @@ public unsafe partial class LPStr public static partial int ReturnLength_IgnoreStringMarshalling(string s) { byte* __s_native = default; - int __retVal; + int __retVal = default; // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.AnsiStringMarshaller.ManagedToUnmanagedIn __s_native__marshaller = new(); try @@ -6438,7 +7528,7 @@ public unsafe partial class LPStr public static partial string Reverse_Return(string s) { byte* __s_native = default; - string __retVal; + string __retVal = default; byte* __retVal_native = default; // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.AnsiStringMarshaller.ManagedToUnmanagedIn __s_native__marshaller = new(); @@ -6629,7 +7719,7 @@ public unsafe partial class BStr public static partial int ReturnLength(string s) { ushort* __s_native = default; - int __retVal; + int __retVal = default; // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.BStrStringMarshaller.ManagedToUnmanagedIn __s_native__marshaller = new(); try @@ -6667,7 +7757,7 @@ public unsafe partial class BStr public static partial int ReturnLength_IgnoreStringMarshalling(string s) { ushort* __s_native = default; - int __retVal; + int __retVal = default; // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.BStrStringMarshaller.ManagedToUnmanagedIn __s_native__marshaller = new(); try @@ -6705,7 +7795,7 @@ public unsafe partial class BStr public static partial string Reverse_Return(string s) { ushort* __s_native = default; - string __retVal; + string __retVal = default; ushort* __retVal_native = default; // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.BStrStringMarshaller.ManagedToUnmanagedIn __s_native__marshaller = new(); @@ -6925,7 +8015,7 @@ public unsafe partial class Utf16 [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial string Reverse_Return(string s) { - string __retVal; + string __retVal = default; ushort* __retVal_native = default; try { @@ -7116,7 +8206,7 @@ public unsafe partial class Utf8 public static partial int ReturnLength(string s) { byte* __s_native = default; - int __retVal; + int __retVal = default; // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn __s_native__marshaller = new(); try @@ -7157,7 +8247,7 @@ public unsafe partial class Utf8 public static partial string Reverse_Return(string s) { byte* __s_native = default; - string __retVal; + string __retVal = default; byte* __retVal_native = default; // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn __s_native__marshaller = new(); From 70c8d81c39f8f0db4992ac6026eb3e503c7644c8 Mon Sep 17 00:00:00 2001 From: Jackson Schuster <36744439+jtschuster@users.noreply.github.com> Date: Mon, 19 Jun 2023 10:46:13 -0700 Subject: [PATCH 2/6] commit1 --- .../LibraryImports.g.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/LibraryImportGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs b/LibraryImportGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs index 7e7c345..71297ca 100644 --- a/LibraryImportGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs +++ b/LibraryImportGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs @@ -462,7 +462,7 @@ public static partial string[] ReverseStrings_Return(string[] strArray, out int // Setup - Perform required setup. int __retVal_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__numElements); - int __retVal_native__lastIndexMarshalled = 0; + ; scoped global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn __strArray_native__marshaller = new(); int __strArray_native__numElements; int __strArray_native__lastIndexMarshalled = 0; @@ -507,7 +507,7 @@ public static partial string[] ReverseStrings_Return(string[] strArray, out int __retVal_native__numElements = numElements; { System.ReadOnlySpan __retVal_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__retVal_native, __retVal_native__numElements); - for (int __i0 = 0; __i0 < __retVal_native__lastIndexMarshalled; ++__i0) + for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) { global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free((ushort*)__retVal_native__nativeSpan[__i0]); } @@ -1148,7 +1148,7 @@ public static partial int[][] TransposeMatrix(int[][] matrix, int[] numRows, int // Setup - Perform required setup. int __retVal_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__numElements); - int __retVal_native__lastIndexMarshalled = 0; + ; scoped global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn __matrix_native__marshaller = new(); int __matrix_native__numElements; int __matrix_native__lastIndexMarshalled = 0; @@ -1202,7 +1202,7 @@ public static partial int[][] TransposeMatrix(int[][] matrix, int[] numRows, int __retVal_native__numElements = numColumns; { System.ReadOnlySpan __retVal_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__retVal_native, __retVal_native__numElements); - for (int __i0 = 0; __i0 < __retVal_native__lastIndexMarshalled; ++__i0) + for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) { int __retVal_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__nativeSpan____i0__numElements); @@ -2678,7 +2678,7 @@ public static partial bool FillRangeArray(global::SharedTypes.IntStructWrapper[] // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn __array_native__marshaller = new(); int __array_native__numElements; - ; + int __array_native__lastIndexMarshalled = 0; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__numElements); try { @@ -2710,7 +2710,7 @@ public static partial bool FillRangeArray(global::SharedTypes.IntStructWrapper[] // Cleanup - Perform required cleanup. { System.ReadOnlySpan __array_native__nativeSpan = __array_native__marshaller.GetUnmanagedValuesDestination(); - for (int __i0 = 0; __i0 < __array_native__nativeSpan.Length; ++__i0) + for (int __i0 = 0; __i0 < __array_native__lastIndexMarshalled; ++__i0) { global::LibraryImportGenerator.IntegrationTests.EnforceAllElementsCleanedUpIntStruct.Free(__array_native__nativeSpan[__i0]); } @@ -2743,7 +2743,7 @@ public static partial bool FillRangeArray2D(global::SharedTypes.IntStructWrapper // Setup - Perform required setup. scoped global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.ManagedToUnmanagedIn __array_native__marshaller = new(); int __array_native__numElements; - ; + int __array_native__lastIndexMarshalled = 0; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__numElements); try { @@ -2788,7 +2788,7 @@ public static partial bool FillRangeArray2D(global::SharedTypes.IntStructWrapper // Cleanup - Perform required cleanup. { System.ReadOnlySpan __array_native__nativeSpan = __array_native__marshaller.GetUnmanagedValuesDestination(); - for (int __i0 = 0; __i0 < __array_native__nativeSpan.Length; ++__i0) + for (int __i0 = 0; __i0 < __array_native__lastIndexMarshalled; ++__i0) { int __array_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); From 42d38fb78ad250b5b742aab151fb0cf61e8fa7a2 Mon Sep 17 00:00:00 2001 From: Jackson Schuster <36744439+jtschuster@users.noreply.github.com> Date: Mon, 19 Jun 2023 12:12:13 -0700 Subject: [PATCH 3/6] commit2 --- ...mInterfaces.ICollectionMarshallingFails.cs | 1 - ...Generator.Tests.ComInterfaces.IIntArray.cs | 2 - ...tor.Tests.ComInterfaces.IJaggedIntArray.cs | 7 -- ...erfaces.IJaggedIntArrayMarshallingFails.cs | 7 -- .../SharedTypes.ComInterfaces.IGetIntArray.cs | 1 - .../ManagedToNativeStubs.g.cs | 2 - .../NativeToManagedStubs.g.cs | 2 - .../LibraryImports.g.cs | 1 - .../LibraryImports.g.cs | 74 ++----------------- 9 files changed, 7 insertions(+), 90 deletions(-) diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.ICollectionMarshallingFails.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.ICollectionMarshallingFails.cs index 30d2ac3..cb158f5 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.ICollectionMarshallingFails.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.ICollectionMarshallingFails.cs @@ -44,7 +44,6 @@ finally { // Cleanup - Perform required cleanup. - __retVal_native__numElements = 10; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); } diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IIntArray.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IIntArray.cs index 5d4874d..f713493 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IIntArray.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IIntArray.cs @@ -40,7 +40,6 @@ finally { // Cleanup - Perform required cleanup. - __retVal_native__numElements = size; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); } @@ -75,7 +74,6 @@ finally { // Cleanup - Perform required cleanup. - __array_native__numElements = __retVal; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__array_native); } diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArray.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArray.cs index e43ae92..88991b7 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArray.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArray.cs @@ -59,9 +59,7 @@ finally { // Cleanup - Perform required cleanup. - __widths_native__numElements = length; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__widths_native); - __retVal_native__numElements = length; { System.ReadOnlySpan __retVal_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__retVal_native, __retVal_native__numElements); for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) @@ -69,7 +67,6 @@ int __retVal_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__nativeSpan____i0__numElements); ; - __retVal_native__nativeSpan____i0__numElements = widths[__i0]; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__retVal_native__nativeSpan[__i0]); } } @@ -127,9 +124,7 @@ finally { // Cleanup - Perform required cleanup. - __widths_native__numElements = __retVal; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__widths_native); - __array_native__numElements = __retVal; { System.ReadOnlySpan __array_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__array_native, __array_native__numElements); for (int __i0 = 0; __i0 < __array_native__nativeSpan.Length; ++__i0) @@ -137,7 +132,6 @@ int __array_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); ; - __array_native__nativeSpan____i0__numElements = widths[__i0]; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__array_native__nativeSpan[__i0]); } } @@ -200,7 +194,6 @@ int __array_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); ; - ; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__array_native__nativeSpan[__i0]); } } diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArrayMarshallingFails.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArrayMarshallingFails.cs index ecf521a..a60242a 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArrayMarshallingFails.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArrayMarshallingFails.cs @@ -66,9 +66,7 @@ finally { // Cleanup - Perform required cleanup. - __widths_native__numElements = length; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__widths_native); - __retVal_native__numElements = length; { System.ReadOnlySpan __retVal_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__retVal_native, __retVal_native__numElements); for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) @@ -76,7 +74,6 @@ int __retVal_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__nativeSpan____i0__numElements); ; - __retVal_native__nativeSpan____i0__numElements = widths[__i0]; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__retVal_native__nativeSpan[__i0]); } } @@ -141,9 +138,7 @@ finally { // Cleanup - Perform required cleanup. - __widths_native__numElements = __retVal; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__widths_native); - __array_native__numElements = __retVal; { System.ReadOnlySpan __array_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__array_native, __array_native__numElements); for (int __i0 = 0; __i0 < __array_native__nativeSpan.Length; ++__i0) @@ -151,7 +146,6 @@ int __array_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); ; - __array_native__nativeSpan____i0__numElements = widths[__i0]; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__array_native__nativeSpan[__i0]); } } @@ -221,7 +215,6 @@ int __array_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); ; - ; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__array_native__nativeSpan[__i0]); } } diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IGetIntArray.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IGetIntArray.cs index f0ee47b..b50e75a 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IGetIntArray.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IGetIntArray.cs @@ -37,7 +37,6 @@ finally { // Cleanup - Perform required cleanup. - __retVal_native__numElements = 10; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); } diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/ManagedToNativeStubs.g.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/ManagedToNativeStubs.g.cs index cdf04c0..8e86040 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/ManagedToNativeStubs.g.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/ManagedToNativeStubs.g.cs @@ -151,7 +151,6 @@ internal unsafe partial interface Native finally { // Cleanup - Perform required cleanup. - __values_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__values_native); } } @@ -485,7 +484,6 @@ internal unsafe partial interface Native { // Cleanup - Perform required cleanup. global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__oldValue_native); - __values_native__numElements = numValues; { System.ReadOnlySpan __values_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__values_native, __values_native__numElements); for (int __i0 = 0; __i0 < __values_native__lastIndexMarshalled; ++__i0) diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/NativeToManagedStubs.g.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/NativeToManagedStubs.g.cs index f4d5267..bfdc4be 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/NativeToManagedStubs.g.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/NativeToManagedStubs.g.cs @@ -153,7 +153,6 @@ internal static void ABI_SumAndSetData(void* __this_native, int** __values_nativ // Cleanup - Perform required cleanup. if (__values_native__ownOriginal) { - __values_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__values_native__original); } } @@ -392,7 +391,6 @@ internal static void ABI_SumAndSetData(void* __this_native, int** __values_nativ // Cleanup - Perform required cleanup. if (__values_native__ownOriginal) { - __values_native__numElements = numValues; { System.ReadOnlySpan __values_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__values_native__original, __values_native__numElements); for (int __i0 = 0; __i0 < __values_native__nativeSpan.Length; ++__i0) diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs index 9cf6893..461e5fd 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs @@ -356,7 +356,6 @@ public static partial int SumAndSetNativeObjectData(void* obj, ref int[] arr, in finally { // Cleanup - Perform required cleanup. - __arr_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__arr_native); } diff --git a/LibraryImportGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs b/LibraryImportGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs index 71297ca..2917b3c 100644 --- a/LibraryImportGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs +++ b/LibraryImportGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs @@ -125,7 +125,6 @@ public static partial void Duplicate(ref int[] values, int numValues) finally { // Cleanup - Perform required cleanup. - __values_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__values_native); } @@ -169,7 +168,6 @@ public static partial int[] CreateRange(int start, int end, out int numValues) finally { // Cleanup - Perform required cleanup. - __retVal_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); } @@ -214,7 +212,6 @@ public static partial void CreateRange_Out(int start, int end, out int numValues finally { // Cleanup - Perform required cleanup. - __res_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__res_native); } @@ -305,7 +302,6 @@ public static partial void ReverseChars(ref char[] chars, int numElements) finally { // Cleanup - Perform required cleanup. - __chars_native__numElements = numElements; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__chars_native); } @@ -426,7 +422,6 @@ public static partial void ReverseStrings_Ref(ref string[] strArray, out int num finally { // Cleanup - Perform required cleanup. - __strArray_native__numElements = numElements; { System.ReadOnlySpan __strArray_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__strArray_native, __strArray_native__numElements); for (int __i0 = 0; __i0 < __strArray_native__lastIndexMarshalled; ++__i0) @@ -504,7 +499,6 @@ public static partial string[] ReverseStrings_Return(string[] strArray, out int finally { // Cleanup - Perform required cleanup. - __retVal_native__numElements = numElements; { System.ReadOnlySpan __retVal_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__retVal_native, __retVal_native__numElements); for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) @@ -592,7 +586,6 @@ public static partial void ReverseStrings_Out(string[] strArray, out int numElem finally { // Cleanup - Perform required cleanup. - __res_native__numElements = numElements; { System.ReadOnlySpan __res_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__res_native, __res_native__numElements); for (int __i0 = 0; __i0 < __res_native__nativeSpan.Length; ++__i0) @@ -650,7 +643,6 @@ public static partial byte[] GetLongBytes(long l) finally { // Cleanup - Perform required cleanup. - __retVal_native__numElements = 8; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); } @@ -694,7 +686,6 @@ public static partial void Append(ref int[] values, int numOriginalValues, int n finally { // Cleanup - Perform required cleanup. - __values_native__numElements = checked(1 + numOriginalValues); global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__values_native); } @@ -982,7 +973,6 @@ public static partial void NegateBools(ref global::SharedTypes.BoolStruct[] bool finally { // Cleanup - Perform required cleanup. - __boolStruct_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__boolStruct_native); } @@ -1050,7 +1040,6 @@ public static partial void NegateBools(global::SharedTypes.BoolStruct[] boolStru finally { // Cleanup - Perform required cleanup. - __pBoolStructOut_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__pBoolStructOut_native); __boolStruct_native__marshaller.Free(); } @@ -1119,7 +1108,6 @@ public unsafe partial class Arrays finally { // Cleanup - Perform required cleanup. - __retVal_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); __boolStruct_native__marshaller.Free(); } @@ -1199,7 +1187,6 @@ public static partial int[][] TransposeMatrix(int[][] matrix, int[] numRows, int finally { // Cleanup - Perform required cleanup. - __retVal_native__numElements = numColumns; { System.ReadOnlySpan __retVal_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__retVal_native, __retVal_native__numElements); for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) @@ -1207,7 +1194,6 @@ public static partial int[][] TransposeMatrix(int[][] matrix, int[] numRows, int int __retVal_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__nativeSpan____i0__numElements); ; - __retVal_native__nativeSpan____i0__numElements = numRows[__i0]; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__retVal_native__nativeSpan[__i0]); } } @@ -1220,7 +1206,6 @@ public static partial int[][] TransposeMatrix(int[][] matrix, int[] numRows, int int __matrix_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __matrix_native__nativeSpan____i0__numElements); ; - ; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__matrix_native__nativeSpan[__i0]); } } @@ -1337,7 +1322,6 @@ public static unsafe partial void Duplicate(ref int*[] values, int numValues) finally { // Cleanup - Perform required cleanup. - __values_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.PointerArrayMarshaller.Free(__values_native); } @@ -1380,7 +1364,6 @@ public unsafe partial class Arrays finally { // Cleanup - Perform required cleanup. - __retVal_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.PointerArrayMarshaller.Free(__retVal_native); } @@ -2371,7 +2354,6 @@ public static partial void MarshalMultidimensionalArray_CheckInnerArraysAreClear int __c_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __c_native__nativeSpan____i0__numElements); ; - ; { System.ReadOnlySpan __c_native__nativeSpan____i0__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination((nint*)__c_native__nativeSpan[__i0], __c_native__nativeSpan____i0__numElements); for (int __i1 = 0; __i1 < __c_native__nativeSpan____i0__nativeSpan.Length; ++__i1) @@ -2441,7 +2423,6 @@ public static partial void MarshalArray_Ref(ref global::SharedTypes.BoolStruct[] finally { // Cleanup - Perform required cleanup. - __c_native__numElements = 10; { System.ReadOnlySpan __c_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__c_native, __c_native__numElements); for (int __i0 = 0; __i0 < __c_native__lastIndexMarshalled; ++__i0) @@ -2517,7 +2498,6 @@ public static partial void NegateBoolsOut(global::SharedTypes.BoolStruct[] boolS finally { // Cleanup - Perform required cleanup. - __pBoolStructOut_native__numElements = numValues; { System.ReadOnlySpan __pBoolStructOut_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__pBoolStructOut_native, __pBoolStructOut_native__numElements); for (int __i0 = 0; __i0 < __pBoolStructOut_native__nativeSpan.Length; ++__i0) @@ -2618,7 +2598,6 @@ public static partial void NegateBoolsOut2D(global::SharedTypes.BoolStruct[][] b finally { // Cleanup - Perform required cleanup. - __pBoolStructOut_native__numElements = length; { System.ReadOnlySpan __pBoolStructOut_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__pBoolStructOut_native, __pBoolStructOut_native__numElements); for (int __i0 = 0; __i0 < __pBoolStructOut_native__nativeSpan.Length; ++__i0) @@ -2626,7 +2605,6 @@ public static partial void NegateBoolsOut2D(global::SharedTypes.BoolStruct[][] b int __pBoolStructOut_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __pBoolStructOut_native__nativeSpan____i0__numElements); ; - __pBoolStructOut_native__nativeSpan____i0__numElements = widths[__i0]; { System.ReadOnlySpan __pBoolStructOut_native__nativeSpan____i0__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination((nint*)__pBoolStructOut_native__nativeSpan[__i0], __pBoolStructOut_native__nativeSpan____i0__numElements); for (int __i1 = 0; __i1 < __pBoolStructOut_native__nativeSpan____i0__nativeSpan.Length; ++__i1) @@ -2647,7 +2625,6 @@ public static partial void NegateBoolsOut2D(global::SharedTypes.BoolStruct[][] b int __boolStruct_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __boolStruct_native__nativeSpan____i0__numElements); ; - ; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((global::SharedTypes.BoolStructMarshaller.BoolStructNative*)__boolStruct_native__nativeSpan[__i0]); } } @@ -2793,7 +2770,6 @@ public static partial bool FillRangeArray2D(global::SharedTypes.IntStructWrapper int __array_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); ; - __array_native__nativeSpan____i0__numElements = widths[__i0]; { System.ReadOnlySpan __array_native__nativeSpan____i0__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination((nint*)__array_native__nativeSpan[__i0], __array_native__nativeSpan____i0__numElements); for (int __i1 = 0; __i1 < __array_native__nativeSpan____i0__nativeSpan.Length; ++__i1) @@ -2864,7 +2840,6 @@ public static partial void NegateBoolsRef(ref global::SharedTypes.BoolStruct[] b finally { // Cleanup - Perform required cleanup. - __boolStruct_native__numElements = numValues; { System.ReadOnlySpan __boolStruct_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__boolStruct_native, __boolStruct_native__numElements); for (int __i0 = 0; __i0 < __boolStruct_native__lastIndexMarshalled; ++__i0) @@ -2930,7 +2905,6 @@ public static partial void NegateBoolsRef2D_LastElementMarshalling(ref global::S finally { // Cleanup - Perform required cleanup. - __boolStruct_native__numElements = length; { System.ReadOnlySpan __boolStruct_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__boolStruct_native, __boolStruct_native__numElements); for (int __i0 = 0; __i0 < __boolStruct_native__lastIndexMarshalled; ++__i0) @@ -3021,7 +2995,6 @@ public static partial void NegateBoolsRef2D_ClearMarshalling(ref global::SharedT finally { // Cleanup - Perform required cleanup. - __boolStruct_native__numElements = length; { System.ReadOnlySpan __boolStruct_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__boolStruct_native, __boolStruct_native__numElements); for (int __i0 = 0; __i0 < __boolStruct_native__lastIndexMarshalled; ++__i0) @@ -3029,7 +3002,6 @@ public static partial void NegateBoolsRef2D_ClearMarshalling(ref global::SharedT int __boolStruct_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __boolStruct_native__nativeSpan____i0__numElements); ; - __boolStruct_native__nativeSpan____i0__numElements = widths[__i0]; { System.ReadOnlySpan __boolStruct_native__nativeSpan____i0__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination((nint*)__boolStruct_native__nativeSpan[__i0], __boolStruct_native__nativeSpan____i0__numElements); for (int __i1 = 0; __i1 < __boolStruct_native__nativeSpan____i0__nativeSpan.Length; ++__i1) @@ -3082,7 +3054,6 @@ public static partial int Sum(global::System.Collections.Generic.List value finally { // Cleanup - Perform required cleanup. - ; global::SharedTypes.ListMarshaller.Free(__values_native); } @@ -3107,26 +3078,18 @@ public unsafe partial class Stateless [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial int SumWithBuffer(global::System.Collections.Generic.List values, int numValues) { - byte* __values_native = default; - int __retVal = default; + byte* __values_native; + int __retVal; // Setup - Perform required setup. int __values_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); ; - try - { - // Marshal - Convert managed data to native data. - System.Span __values_native__buffer = stackalloc byte[global::SharedTypes.ListMarshallerWithBuffer.BufferSize]; - __values_native = global::SharedTypes.ListMarshallerWithBuffer.AllocateContainerForUnmanagedElements(values, __values_native__buffer, out __values_native__numElements); - global::SharedTypes.ListMarshallerWithBuffer.GetManagedValuesSource(values).CopyTo(global::SharedTypes.ListMarshallerWithBuffer.GetUnmanagedValuesDestination(__values_native, __values_native__numElements)); - { - __retVal = __PInvoke(__values_native, numValues); - } - } - finally + // Marshal - Convert managed data to native data. + System.Span __values_native__buffer = stackalloc byte[global::SharedTypes.ListMarshallerWithBuffer.BufferSize]; + __values_native = global::SharedTypes.ListMarshallerWithBuffer.AllocateContainerForUnmanagedElements(values, __values_native__buffer, out __values_native__numElements); + global::SharedTypes.ListMarshallerWithBuffer.GetManagedValuesSource(values).CopyTo(global::SharedTypes.ListMarshallerWithBuffer.GetUnmanagedValuesDestination(__values_native, __values_native__numElements)); { - // Cleanup - Perform required cleanup. - ; + __retVal = __PInvoke(__values_native, numValues); } return __retVal; @@ -3176,7 +3139,6 @@ public static unsafe partial int SumWithFreeTracking(global::System.Collections. finally { // Cleanup - Perform required cleanup. - ; { System.ReadOnlySpan __values_native__nativeSpan = global::SharedTypes.ListMarshaller.GetUnmanagedValuesDestination(__values_native, __values_native__numElements); for (int __i0 = 0; __i0 < __values_native__lastIndexMarshalled; ++__i0) @@ -3255,7 +3217,6 @@ public static partial int SumInArray(in global::System.Collections.Generic.List< finally { // Cleanup - Perform required cleanup. - ; global::SharedTypes.ListMarshaller.Free(__values_native); } @@ -3302,7 +3263,6 @@ public static partial void Duplicate(ref global::System.Collections.Generic.List finally { // Cleanup - Perform required cleanup. - __values_native__numElements = numValues; global::SharedTypes.ListMarshaller.Free(__values_native); } @@ -3349,7 +3309,6 @@ public unsafe partial class Stateless finally { // Cleanup - Perform required cleanup. - __retVal_native__numElements = numValues; global::SharedTypes.ListMarshaller.Free(__retVal_native); } @@ -3397,7 +3356,6 @@ public static partial void CreateRange_Out(int start, int end, out int numValues finally { // Cleanup - Perform required cleanup. - __res_native__numElements = numValues; global::SharedTypes.ListMarshaller.Free(__res_native); } @@ -3441,7 +3399,6 @@ public unsafe partial class Stateless finally { // Cleanup - Perform required cleanup. - __retVal_native__numElements = 8; global::SharedTypes.ListMarshaller.Free(__retVal_native); } @@ -3496,7 +3453,6 @@ public static partial bool AndAllMembers(global::System.Collections.Generic.List finally { // Cleanup - Perform required cleanup. - ; global::SharedTypes.ListMarshaller.Free(__pArray_native); } @@ -3551,7 +3507,6 @@ public static partial bool AndAllMembersIn(in global::System.Collections.Generic finally { // Cleanup - Perform required cleanup. - ; global::SharedTypes.ListMarshaller.Free(__pArray_native); } @@ -3613,7 +3568,6 @@ public static partial void NegateBools(ref global::System.Collections.Generic.Li finally { // Cleanup - Perform required cleanup. - __boolStruct_native__numElements = numValues; global::SharedTypes.ListMarshaller.Free(__boolStruct_native); } @@ -3679,9 +3633,7 @@ public static partial void NegateBools(global::System.Collections.Generic.List.Free(__pBoolStructOut_native); - ; global::SharedTypes.ListMarshaller.Free(__boolStruct_native); } @@ -3747,9 +3699,7 @@ public unsafe partial class Stateless finally { // Cleanup - Perform required cleanup. - __retVal_native__numElements = numValues; global::SharedTypes.ListMarshaller.Free(__retVal_native); - ; global::SharedTypes.ListMarshaller.Free(__boolStruct_native); } @@ -3801,9 +3751,6 @@ public static partial int GuaranteedUnmarshal(out global::System.Collections.Gen __ret_native__numElements = 1; ret = global::LibraryImportGenerator.IntegrationTests.NativeExportsNE.Collections.Stateless.ListGuaranteedUnmarshal.AllocateContainerForManagedElementsFinally(__ret_native, __ret_native__numElements); } - - // Cleanup - Perform required cleanup. - __ret_native__numElements = 1; } return __retVal; @@ -3854,9 +3801,6 @@ public static partial int GuaranteedUnmarshal(out global::System.Collections.Gen __ret_native__numElements = 1; ret = global::LibraryImportGenerator.IntegrationTests.NativeExportsNE.Collections.Stateless.ListGuaranteedUnmarshal.AllocateContainerForManagedElementsFinally(__ret_native, __ret_native__numElements); } - - // Cleanup - Perform required cleanup. - __ret_native__numElements = 1; } return __retVal; @@ -6097,7 +6041,6 @@ public static partial void Duplicate(ref global::System.Span values, int nu finally { // Cleanup - Perform required cleanup. - __values_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.SpanMarshaller.Free(__values_native); } @@ -6141,7 +6084,6 @@ public unsafe partial class Span finally { // Cleanup - Perform required cleanup. - __retVal_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.SpanMarshaller.Free(__retVal_native); } @@ -6186,7 +6128,6 @@ public static partial void CreateRange_Out(int start, int end, out int numValues finally { // Cleanup - Perform required cleanup. - __res_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.SpanMarshaller.Free(__res_native); } @@ -6227,7 +6168,6 @@ public unsafe partial class Span finally { // Cleanup - Perform required cleanup. - __retVal_native__numElements = 8; global::System.Runtime.InteropServices.Marshalling.SpanMarshaller.Free(__retVal_native); } From cf480c199490c85009025e1afc128319f20f1b98 Mon Sep 17 00:00:00 2001 From: Jackson Schuster <36744439+jtschuster@users.noreply.github.com> Date: Mon, 19 Jun 2023 13:48:15 -0700 Subject: [PATCH 4/6] commit3 --- ...terfaceGenerator.Tests.ComInterfaces.IJaggedIntArray.cs | 3 +++ ....Tests.ComInterfaces.IJaggedIntArrayMarshallingFails.cs | 3 +++ .../LibraryImports.g.cs | 7 +++++++ 3 files changed, 13 insertions(+) diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArray.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArray.cs index 88991b7..3acf74d 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArray.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArray.cs @@ -67,6 +67,7 @@ int __retVal_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__nativeSpan____i0__numElements); ; + __retVal_native__nativeSpan____i0__numElements = widths[__i0]; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__retVal_native__nativeSpan[__i0]); } } @@ -132,6 +133,7 @@ int __array_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); ; + __array_native__nativeSpan____i0__numElements = widths[__i0]; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__array_native__nativeSpan[__i0]); } } @@ -194,6 +196,7 @@ int __array_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); ; + __array_native__nativeSpan____i0__numElements = 0; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__array_native__nativeSpan[__i0]); } } diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArrayMarshallingFails.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArrayMarshallingFails.cs index a60242a..d83e0c6 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArrayMarshallingFails.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArrayMarshallingFails.cs @@ -74,6 +74,7 @@ int __retVal_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__nativeSpan____i0__numElements); ; + __retVal_native__nativeSpan____i0__numElements = widths[__i0]; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__retVal_native__nativeSpan[__i0]); } } @@ -146,6 +147,7 @@ int __array_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); ; + __array_native__nativeSpan____i0__numElements = widths[__i0]; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__array_native__nativeSpan[__i0]); } } @@ -215,6 +217,7 @@ int __array_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); ; + __array_native__nativeSpan____i0__numElements = 0; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__array_native__nativeSpan[__i0]); } } diff --git a/LibraryImportGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs b/LibraryImportGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs index 2917b3c..1eac78c 100644 --- a/LibraryImportGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs +++ b/LibraryImportGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs @@ -1194,6 +1194,7 @@ public static partial int[][] TransposeMatrix(int[][] matrix, int[] numRows, int int __retVal_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__nativeSpan____i0__numElements); ; + __retVal_native__nativeSpan____i0__numElements = numRows[__i0]; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__retVal_native__nativeSpan[__i0]); } } @@ -1206,6 +1207,7 @@ public static partial int[][] TransposeMatrix(int[][] matrix, int[] numRows, int int __matrix_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __matrix_native__nativeSpan____i0__numElements); ; + __matrix_native__nativeSpan____i0__numElements = 0; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__matrix_native__nativeSpan[__i0]); } } @@ -2354,6 +2356,7 @@ public static partial void MarshalMultidimensionalArray_CheckInnerArraysAreClear int __c_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __c_native__nativeSpan____i0__numElements); ; + __c_native__nativeSpan____i0__numElements = 0; { System.ReadOnlySpan __c_native__nativeSpan____i0__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination((nint*)__c_native__nativeSpan[__i0], __c_native__nativeSpan____i0__numElements); for (int __i1 = 0; __i1 < __c_native__nativeSpan____i0__nativeSpan.Length; ++__i1) @@ -2605,6 +2608,7 @@ public static partial void NegateBoolsOut2D(global::SharedTypes.BoolStruct[][] b int __pBoolStructOut_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __pBoolStructOut_native__nativeSpan____i0__numElements); ; + __pBoolStructOut_native__nativeSpan____i0__numElements = widths[__i0]; { System.ReadOnlySpan __pBoolStructOut_native__nativeSpan____i0__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination((nint*)__pBoolStructOut_native__nativeSpan[__i0], __pBoolStructOut_native__nativeSpan____i0__numElements); for (int __i1 = 0; __i1 < __pBoolStructOut_native__nativeSpan____i0__nativeSpan.Length; ++__i1) @@ -2625,6 +2629,7 @@ public static partial void NegateBoolsOut2D(global::SharedTypes.BoolStruct[][] b int __boolStruct_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __boolStruct_native__nativeSpan____i0__numElements); ; + __boolStruct_native__nativeSpan____i0__numElements = 0; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((global::SharedTypes.BoolStructMarshaller.BoolStructNative*)__boolStruct_native__nativeSpan[__i0]); } } @@ -2770,6 +2775,7 @@ public static partial bool FillRangeArray2D(global::SharedTypes.IntStructWrapper int __array_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); ; + __array_native__nativeSpan____i0__numElements = widths[__i0]; { System.ReadOnlySpan __array_native__nativeSpan____i0__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination((nint*)__array_native__nativeSpan[__i0], __array_native__nativeSpan____i0__numElements); for (int __i1 = 0; __i1 < __array_native__nativeSpan____i0__nativeSpan.Length; ++__i1) @@ -3002,6 +3008,7 @@ public static partial void NegateBoolsRef2D_ClearMarshalling(ref global::SharedT int __boolStruct_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __boolStruct_native__nativeSpan____i0__numElements); ; + __boolStruct_native__nativeSpan____i0__numElements = widths[__i0]; { System.ReadOnlySpan __boolStruct_native__nativeSpan____i0__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination((nint*)__boolStruct_native__nativeSpan[__i0], __boolStruct_native__nativeSpan____i0__numElements); for (int __i1 = 0; __i1 < __boolStruct_native__nativeSpan____i0__nativeSpan.Length; ++__i1) From a7207b7555b450b024da8208931351bec25159ff Mon Sep 17 00:00:00 2001 From: Jackson Schuster <36744439+jtschuster@users.noreply.github.com> Date: Mon, 19 Jun 2023 15:04:47 -0700 Subject: [PATCH 5/6] commit4 --- ...omInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArray.cs | 3 --- ...ator.Tests.ComInterfaces.IJaggedIntArrayMarshallingFails.cs | 3 --- .../LibraryImports.g.cs | 3 --- 3 files changed, 9 deletions(-) diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArray.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArray.cs index 3acf74d..88991b7 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArray.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArray.cs @@ -67,7 +67,6 @@ int __retVal_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__nativeSpan____i0__numElements); ; - __retVal_native__nativeSpan____i0__numElements = widths[__i0]; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__retVal_native__nativeSpan[__i0]); } } @@ -133,7 +132,6 @@ int __array_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); ; - __array_native__nativeSpan____i0__numElements = widths[__i0]; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__array_native__nativeSpan[__i0]); } } @@ -196,7 +194,6 @@ int __array_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); ; - __array_native__nativeSpan____i0__numElements = 0; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__array_native__nativeSpan[__i0]); } } diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArrayMarshallingFails.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArrayMarshallingFails.cs index d83e0c6..a60242a 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArrayMarshallingFails.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/ComInterfaceGenerator.Tests.ComInterfaces.IJaggedIntArrayMarshallingFails.cs @@ -74,7 +74,6 @@ int __retVal_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__nativeSpan____i0__numElements); ; - __retVal_native__nativeSpan____i0__numElements = widths[__i0]; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__retVal_native__nativeSpan[__i0]); } } @@ -147,7 +146,6 @@ int __array_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); ; - __array_native__nativeSpan____i0__numElements = widths[__i0]; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__array_native__nativeSpan[__i0]); } } @@ -217,7 +215,6 @@ int __array_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); ; - __array_native__nativeSpan____i0__numElements = 0; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__array_native__nativeSpan[__i0]); } } diff --git a/LibraryImportGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs b/LibraryImportGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs index 1eac78c..e62fce5 100644 --- a/LibraryImportGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs +++ b/LibraryImportGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs @@ -1194,7 +1194,6 @@ public static partial int[][] TransposeMatrix(int[][] matrix, int[] numRows, int int __retVal_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__nativeSpan____i0__numElements); ; - __retVal_native__nativeSpan____i0__numElements = numRows[__i0]; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__retVal_native__nativeSpan[__i0]); } } @@ -1207,7 +1206,6 @@ public static partial int[][] TransposeMatrix(int[][] matrix, int[] numRows, int int __matrix_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __matrix_native__nativeSpan____i0__numElements); ; - __matrix_native__nativeSpan____i0__numElements = 0; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__matrix_native__nativeSpan[__i0]); } } @@ -2629,7 +2627,6 @@ public static partial void NegateBoolsOut2D(global::SharedTypes.BoolStruct[][] b int __boolStruct_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __boolStruct_native__nativeSpan____i0__numElements); ; - __boolStruct_native__nativeSpan____i0__numElements = 0; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((global::SharedTypes.BoolStructMarshaller.BoolStructNative*)__boolStruct_native__nativeSpan[__i0]); } } From 8b125f3a504007a0a0cbc23af32e80d63b9cabf1 Mon Sep 17 00:00:00 2001 From: Jackson Schuster <36744439+jtschuster@users.noreply.github.com> Date: Wed, 2 Aug 2023 10:26:51 -0700 Subject: [PATCH 6/6] Add CleanuoOut changes --- ...ComInterfaces.IArrayOfStatelessElements.cs | 33 +- ...nterfaces.ICustomStringMarshallingUtf16.cs | 9 +- .../SharedTypes.ComInterfaces.IDerived.cs | 9 +- .../SharedTypes.ComInterfaces.IEnumUnknown.cs | 11 +- .../SharedTypes.ComInterfaces.IGetIntArray.cs | 11 +- .../SharedTypes.ComInterfaces.IIntArray.cs | 28 +- .../SharedTypes.ComInterfaces.IInterface.cs | 26 +- ...aredTypes.ComInterfaces.IJaggedIntArray.cs | 60 +- .../SharedTypes.ComInterfaces.IRefStrings.cs | 15 +- ...aredTypes.ComInterfaces.ISafeFileHandle.cs | 15 +- ...tatefulCallerAllocatedBufferMarshalling.cs | 45 +- ...ces.IStatefulCollectionBlittableElement.cs | 45 +- ...ces.IStatefulCollectionStatelessElement.cs | 69 +- ...mInterfaces.IStatefulFinallyMarshalling.cs | 33 +- ...ypes.ComInterfaces.IStatefulMarshalling.cs | 45 +- ...omInterfaces.IStatefulPinnedMarshalling.cs | 45 +- ...tatelessCallerAllocateBufferMarshalling.cs | 35 +- ...es.IStatelessCollectionBlittableElement.cs | 41 +- ...es.IStatelessCollectionStatelessElement.cs | 65 +- ...Interfaces.IStatelessFinallyMarshalling.cs | 23 +- ...pes.ComInterfaces.IStatelessMarshalling.cs | 35 +- ...mInterfaces.IStatelessPinnedMarshalling.cs | 33 +- ...omInterfaces.IStringMarshallingOverride.cs | 29 +- ...faces.IStringMarshallingOverrideDerived.cs | 58 +- ...edTypes.ComInterfaces.IUTF16Marshalling.cs | 9 +- ...redTypes.ComInterfaces.IUTF8Marshalling.cs | 11 +- ...allingFails.ICollectionMarshallingFails.cs | 122 ++- ...ngFails.IJaggedIntArrayMarshallingFails.cs | 207 ++++- ...llingFails.IStringArrayMarshallingFails.cs | 34 +- .../ManagedToNativeStubs.g.cs | 39 +- .../NativeToManagedStubs.g.cs | 12 +- .../LibraryImports.g.cs | 33 +- .../LibraryImports.g.cs | 793 ++++++++++++------ 33 files changed, 1463 insertions(+), 615 deletions(-) diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IArrayOfStatelessElements.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IArrayOfStatelessElements.cs index b187078..d13cd9a 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IArrayOfStatelessElements.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IArrayOfStatelessElements.cs @@ -50,7 +50,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __param_native__nativeSpan = __param_native__marshaller.GetUnmanagedValuesDestination(); for (int __i0 = 0; __i0 < __param_native__lastIndexMarshalled; ++__i0) @@ -102,7 +102,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __param_native__nativeSpan = __param_native__marshaller.GetUnmanagedValuesDestination(); for (int __i0 = 0; __i0 < __param_native__lastIndexMarshalled; ++__i0) @@ -120,6 +120,7 @@ void global::SharedTypes.ComInterfaces.IArrayOfStatelessElements.MethodOut(out global::SharedTypes.ComInterfaces.StatelessType[] param, int size) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IArrayOfStatelessElements)); + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out param); nint* __param_native = default; int __invokeRetVal = default; @@ -132,6 +133,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[5])(__this, &__param_native, size); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -148,17 +150,20 @@ } finally { - // Cleanup - Perform required cleanup. + if (__invokeSucceeded) { - System.ReadOnlySpan __param_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__param_native, __param_native__numElements); - for (int __i0 = 0; __i0 < __param_native__nativeSpan.Length; ++__i0) + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. { - global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__param_native__nativeSpan[__i0]); + System.ReadOnlySpan __param_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__param_native, __param_native__numElements); + for (int __i0 = 0; __i0 < __param_native__nativeSpan.Length; ++__i0) + { + global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__param_native__nativeSpan[__i0]); + } } - } - __param_native__numElements = size; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__param_native); + __param_native__numElements = size; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__param_native); + } } } @@ -206,7 +211,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __param_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__param_native, __param_native__numElements); for (int __i0 = 0; __i0 < __param_native__lastIndexMarshalled; ++__i0) @@ -259,7 +264,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __param_native__nativeSpan = __param_native__marshaller.GetUnmanagedValuesDestination(); for (int __i0 = 0; __i0 < __param_native__lastIndexMarshalled; ++__i0) @@ -311,7 +316,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __param_native__nativeSpan = __param_native__marshaller.GetUnmanagedValuesDestination(); for (int __i0 = 0; __i0 < __param_native__nativeSpan.Length; ++__i0) @@ -372,7 +377,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __param_native__nativeSpan = __param_native__marshaller.GetUnmanagedValuesDestination(); for (int __i0 = 0; __i0 < __param_native__lastIndexMarshalled; ++__i0) @@ -543,7 +548,7 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. if (__param_native__ownOriginal) { { diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.ICustomStringMarshallingUtf16.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.ICustomStringMarshallingUtf16.cs index 350e9ab..4628b91 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.ICustomStringMarshallingUtf16.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.ICustomStringMarshallingUtf16.cs @@ -16,6 +16,7 @@ string global::SharedTypes.ComInterfaces.ICustomStringMarshallingUtf16.GetString() { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.ICustomStringMarshallingUtf16)); + bool __invokeSucceeded = default; string __retVal = default; ushort* __retVal_native = default; int __invokeRetVal = default; @@ -25,6 +26,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[3])(__this, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -32,8 +34,11 @@ } finally { - // Cleanup - Perform required cleanup. - global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__retVal_native); + } } return __retVal; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IDerived.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IDerived.cs index 68edcfb..5f29d6c 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IDerived.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IDerived.cs @@ -33,6 +33,7 @@ string global::SharedTypes.ComInterfaces.IDerived.GetName() { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IDerived)); + bool __invokeSucceeded = default; string __retVal = default; ushort* __retVal_native = default; int __invokeRetVal = default; @@ -42,6 +43,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[6])(__this, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -49,8 +51,11 @@ } finally { - // Cleanup - Perform required cleanup. - global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__retVal_native); + } } return __retVal; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IEnumUnknown.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IEnumUnknown.cs index ec54555..27add5f 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IEnumUnknown.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IEnumUnknown.cs @@ -52,7 +52,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __rgelt_native__nativeSpan = __rgelt_native__marshaller.GetUnmanagedValuesDestination(); for (int __i0 = 0; __i0 < __rgelt_native__nativeSpan.Length; ++__i0) @@ -100,6 +100,7 @@ void global::SharedTypes.ComInterfaces.IEnumUnknown.Clone(out global::SharedTypes.ComInterfaces.IEnumUnknown ppenum) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IEnumUnknown)); + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out ppenum); void* __ppenum_native = default; int __invokeRetVal = default; @@ -109,6 +110,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[6])(__this, &__ppenum_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -116,8 +118,11 @@ } finally { - // Cleanup - Perform required cleanup. - global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.Free(__ppenum_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.Free(__ppenum_native); + } } } } diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IGetIntArray.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IGetIntArray.cs index 946449e..21059d5 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IGetIntArray.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IGetIntArray.cs @@ -16,6 +16,7 @@ int[] global::SharedTypes.ComInterfaces.IGetIntArray.GetInts() { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IGetIntArray)); + bool __invokeSucceeded = default; int[] __retVal = default; int* __retVal_native = default; int __invokeRetVal = default; @@ -28,6 +29,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[3])(__this, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -37,9 +39,12 @@ } finally { - // Cleanup - Perform required cleanup. - __retVal_native__numElements = 10; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__numElements = 10; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); + } } return __retVal; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IIntArray.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IIntArray.cs index 8922540..52127f7 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IIntArray.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IIntArray.cs @@ -16,6 +16,7 @@ int[] global::SharedTypes.ComInterfaces.IIntArray.GetReturn(out int size) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IIntArray)); + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out size); int[] __retVal = default; int* __retVal_native = default; @@ -31,6 +32,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[3])(__this, __size_native, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -40,9 +42,12 @@ } finally { - // Cleanup - Perform required cleanup. - __retVal_native__numElements = size; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__numElements = size; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); + } } return __retVal; @@ -53,6 +58,7 @@ int global::SharedTypes.ComInterfaces.IIntArray.GetOut(out int[] array) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IIntArray)); + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out array); int* __array_native = default; int __retVal = default; @@ -66,6 +72,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[4])(__this, &__array_native, &__retVal); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -75,9 +82,12 @@ } finally { - // Cleanup - Perform required cleanup. - __array_native__numElements = __retVal; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__array_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __array_native__numElements = __retVal; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__array_native); + } } return __retVal; @@ -164,7 +174,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __array_native__marshaller.Free(); } } @@ -197,7 +207,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __array_native__numElements = size; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__array_native); } @@ -409,7 +419,7 @@ internal static int ABI_SwapArray(System.Runtime.InteropServices.ComWrappers.Com } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. if (__array_native__ownOriginal) { __array_native__numElements = size; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IInterface.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IInterface.cs index ff6245f..0d9e729 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IInterface.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IInterface.cs @@ -16,6 +16,7 @@ global::SharedTypes.ComInterfaces.IInt global::SharedTypes.ComInterfaces.IInterface.Get() { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IInterface)); + bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.IInt __retVal = default; void* __retVal_native = default; int __invokeRetVal = default; @@ -25,6 +26,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[3])(__this, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -32,8 +34,11 @@ } finally { - // Cleanup - Perform required cleanup. - global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.Free(__retVal_native); + } } return __retVal; @@ -60,7 +65,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.Free(__value_native); } } @@ -87,7 +92,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.Free(__value_native); } } @@ -97,6 +102,7 @@ void global::SharedTypes.ComInterfaces.IInterface.GetOut(out global::SharedTypes.ComInterfaces.IInt value) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IInterface)); + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out value); void* __value_native = default; int __invokeRetVal = default; @@ -106,6 +112,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[6])(__this, &__value_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -113,8 +120,11 @@ } finally { - // Cleanup - Perform required cleanup. - global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.Free(__value_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.Free(__value_native); + } } } @@ -139,7 +149,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.Free(__value_native); } } @@ -220,7 +230,7 @@ internal static int ABI_SwapRef(System.Runtime.InteropServices.ComWrappers.ComIn } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. if (__value_native__ownOriginal) { global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.Free(__value_native__original); diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IJaggedIntArray.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IJaggedIntArray.cs index f7d3efb..06b6def 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IJaggedIntArray.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IJaggedIntArray.cs @@ -16,6 +16,7 @@ int[][] global::SharedTypes.ComInterfaces.IJaggedIntArray.Get(out int[] widths, out int length) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IJaggedIntArray)); + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out widths); System.Runtime.CompilerServices.Unsafe.SkipInit(out length); int* __widths_native = default; @@ -35,6 +36,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[3])(__this, &__widths_native, __length_native, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -58,22 +60,25 @@ } finally { - // Cleanup - Perform required cleanup. - __widths_native__numElements = length; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__widths_native); + if (__invokeSucceeded) { - System.ReadOnlySpan __retVal_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__retVal_native, __retVal_native__numElements); - for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __widths_native__numElements = length; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__widths_native); { - int __retVal_native__nativeSpan____i0__numElements; - System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__nativeSpan____i0__numElements); - __retVal_native__nativeSpan____i0__numElements = widths[__i0]; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__retVal_native__nativeSpan[__i0]); + System.ReadOnlySpan __retVal_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__retVal_native, __retVal_native__numElements); + for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) + { + int __retVal_native__nativeSpan____i0__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__nativeSpan____i0__numElements); + __retVal_native__nativeSpan____i0__numElements = widths[__i0]; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__retVal_native__nativeSpan[__i0]); + } } - } - __retVal_native__numElements = length; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); + __retVal_native__numElements = length; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); + } } return __retVal; @@ -84,6 +89,7 @@ int global::SharedTypes.ComInterfaces.IJaggedIntArray.Get2(out int[][] array, out int[] widths) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IJaggedIntArray)); + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out array); System.Runtime.CompilerServices.Unsafe.SkipInit(out widths); System.IntPtr* __array_native = default; @@ -101,6 +107,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[4])(__this, &__array_native, &__widths_native, &__retVal); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -124,22 +131,25 @@ } finally { - // Cleanup - Perform required cleanup. - __widths_native__numElements = __retVal; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__widths_native); + if (__invokeSucceeded) { - System.ReadOnlySpan __array_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__array_native, __array_native__numElements); - for (int __i0 = 0; __i0 < __array_native__nativeSpan.Length; ++__i0) + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __widths_native__numElements = __retVal; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__widths_native); { - int __array_native__nativeSpan____i0__numElements; - System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); - __array_native__nativeSpan____i0__numElements = widths[__i0]; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__array_native__nativeSpan[__i0]); + System.ReadOnlySpan __array_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__array_native, __array_native__numElements); + for (int __i0 = 0; __i0 < __array_native__nativeSpan.Length; ++__i0) + { + int __array_native__nativeSpan____i0__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); + __array_native__nativeSpan____i0__numElements = widths[__i0]; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__array_native__nativeSpan[__i0]); + } } - } - __array_native__numElements = __retVal; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__array_native); + __array_native__numElements = __retVal; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__array_native); + } } return __retVal; @@ -188,7 +198,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __array_native__nativeSpan = __array_native__marshaller.GetUnmanagedValuesDestination(); for (int __i0 = 0; __i0 < __array_native__lastIndexMarshalled; ++__i0) diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IRefStrings.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IRefStrings.cs index a80f5a5..39b5a21 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IRefStrings.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IRefStrings.cs @@ -33,7 +33,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.Free(__value_native); } } @@ -63,7 +63,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __value_native__marshaller.Free(); } } @@ -73,6 +73,7 @@ void global::SharedTypes.ComInterfaces.IRefStrings.OutString(out string value) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IRefStrings)); + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out value); byte* __value_native = default; int __invokeRetVal = default; @@ -82,6 +83,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[5])(__this, &__value_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -89,8 +91,11 @@ } finally { - // Cleanup - Perform required cleanup. - global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.Free(__value_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.Free(__value_native); + } } } } @@ -124,7 +129,7 @@ internal static int ABI_RefString(System.Runtime.InteropServices.ComWrappers.Com } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. if (__value_native__ownOriginal) { global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.Free(__value_native__original); diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.ISafeFileHandle.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.ISafeFileHandle.cs index 6cf4ae5..7677cd5 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.ISafeFileHandle.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.ISafeFileHandle.cs @@ -34,7 +34,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __p_native__marshaller.Free(); } } @@ -64,7 +64,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __p_native__marshaller.Free(); } } @@ -74,6 +74,7 @@ void global::SharedTypes.ComInterfaces.ISafeFileHandle.MethodOut(out global::Microsoft.Win32.SafeHandles.SafeFileHandle p) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.ISafeFileHandle)); + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out p); nint __p_native = default; int __invokeRetVal = default; @@ -85,6 +86,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[5])(__this, &__p_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // UnmarshalCapture - Capture the native data into marshaller instances in case conversion to managed data throws an exception. __p_native__marshaller.FromUnmanaged(__p_native); @@ -94,8 +96,11 @@ } finally { - // Cleanup - Perform required cleanup. - __p_native__marshaller.Free(); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __p_native__marshaller.Free(); + } } } @@ -136,7 +141,7 @@ p = __p_native__marshaller.ToManagedFinally(); } - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __p_native__marshaller.Free(); } } diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulCallerAllocatedBufferMarshalling.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulCallerAllocatedBufferMarshalling.cs index 6d3e9b1..5671126 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulCallerAllocatedBufferMarshalling.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulCallerAllocatedBufferMarshalling.cs @@ -36,7 +36,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __param_native__marshaller.Free(); } } @@ -66,7 +66,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __param_native__marshaller.Free(); } } @@ -76,6 +76,7 @@ void global::SharedTypes.ComInterfaces.IStatefulCallerAllocatedBufferMarshalling.MethodOut(out global::SharedTypes.ComInterfaces.StatefulCallerAllocatedBufferType param) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatefulCallerAllocatedBufferMarshalling)); + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out param); nint __param_native = default; int __invokeRetVal = default; @@ -87,6 +88,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[5])(__this, &__param_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // UnmarshalCapture - Capture the native data into marshaller instances in case conversion to managed data throws an exception. __param_native__marshaller.FromUnmanaged(__param_native); @@ -96,8 +98,11 @@ } finally { - // Cleanup - Perform required cleanup. - __param_native__marshaller.Free(); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __param_native__marshaller.Free(); + } } } @@ -127,7 +132,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __param_native__marshaller.Free(); } } @@ -137,6 +142,7 @@ global::SharedTypes.ComInterfaces.StatefulCallerAllocatedBufferType global::SharedTypes.ComInterfaces.IStatefulCallerAllocatedBufferMarshalling.Return() { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatefulCallerAllocatedBufferMarshalling)); + bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.StatefulCallerAllocatedBufferType __retVal = default; nint __retVal_native = default; int __invokeRetVal = default; @@ -148,6 +154,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[7])(__this, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // UnmarshalCapture - Capture the native data into marshaller instances in case conversion to managed data throws an exception. __retVal_native__marshaller.FromUnmanaged(__retVal_native); @@ -157,8 +164,11 @@ } finally { - // Cleanup - Perform required cleanup. - __retVal_native__marshaller.Free(); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__marshaller.Free(); + } } return __retVal; @@ -169,6 +179,7 @@ global::SharedTypes.ComInterfaces.StatefulCallerAllocatedBufferType global::SharedTypes.ComInterfaces.IStatefulCallerAllocatedBufferMarshalling.ReturnPreserveSig() { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatefulCallerAllocatedBufferMarshalling)); + bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.StatefulCallerAllocatedBufferType __retVal = default; nint __retVal_native = default; // Setup - Perform required setup. @@ -179,6 +190,7 @@ __retVal_native = ((delegate* unmanaged[MemberFunction] )__vtable_native[8])(__this); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // UnmarshalCapture - Capture the native data into marshaller instances in case conversion to managed data throws an exception. __retVal_native__marshaller.FromUnmanaged(__retVal_native); @@ -187,8 +199,11 @@ } finally { - // Cleanup - Perform required cleanup. - __retVal_native__marshaller.Free(); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__marshaller.Free(); + } } return __retVal; @@ -221,7 +236,7 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __param_native__marshaller.Free(); } @@ -253,7 +268,7 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __param_native__marshaller.Free(); } @@ -284,7 +299,7 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __param_native__marshaller.Free(); } @@ -318,7 +333,7 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __param_native__marshaller.Free(); } @@ -349,7 +364,7 @@ internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInt } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __invokeRetValUnmanaged__marshaller.Free(); } @@ -378,7 +393,7 @@ internal static nint ABI_ReturnPreserveSig(System.Runtime.InteropServices.ComWra } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __retVal_native__marshaller.Free(); } diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulCollectionBlittableElement.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulCollectionBlittableElement.cs index f59e629..28d26a7 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulCollectionBlittableElement.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulCollectionBlittableElement.cs @@ -39,7 +39,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __p_native__marshaller.Free(); } } @@ -74,7 +74,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __pIn_native__marshaller.Free(); } } @@ -112,7 +112,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __pRef_native__marshaller.Free(); } } @@ -122,6 +122,7 @@ void global::SharedTypes.ComInterfaces.IStatefulCollectionBlittableElement.MethodOut(out global::SharedTypes.ComInterfaces.StatefulCollection pOut, out int size) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatefulCollectionBlittableElement)); + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out pOut); System.Runtime.CompilerServices.Unsafe.SkipInit(out size); byte* __pOut_native = default; @@ -138,6 +139,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[6])(__this, &__pOut_native, __size_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // UnmarshalCapture - Capture the native data into marshaller instances in case conversion to managed data throws an exception. __pOut_native__marshaller.FromUnmanaged(__pOut_native); @@ -149,8 +151,11 @@ } finally { - // Cleanup - Perform required cleanup. - __pOut_native__marshaller.Free(); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __pOut_native__marshaller.Free(); + } } } @@ -159,6 +164,7 @@ global::SharedTypes.ComInterfaces.StatefulCollection global::SharedTypes.ComInterfaces.IStatefulCollectionBlittableElement.Return(int size) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatefulCollectionBlittableElement)); + bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.StatefulCollection __retVal = default; byte* __retVal_native = default; int __invokeRetVal = default; @@ -172,6 +178,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[7])(__this, size, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // UnmarshalCapture - Capture the native data into marshaller instances in case conversion to managed data throws an exception. __retVal_native__marshaller.FromUnmanaged(__retVal_native); @@ -183,8 +190,11 @@ } finally { - // Cleanup - Perform required cleanup. - __retVal_native__marshaller.Free(); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__marshaller.Free(); + } } return __retVal; @@ -195,6 +205,7 @@ global::SharedTypes.ComInterfaces.StatefulCollection global::SharedTypes.ComInterfaces.IStatefulCollectionBlittableElement.ReturnPreserveSig(int size) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatefulCollectionBlittableElement)); + bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.StatefulCollection __retVal = default; byte* __retVal_native = default; // Setup - Perform required setup. @@ -207,6 +218,7 @@ __retVal_native = ((delegate* unmanaged[MemberFunction] )__vtable_native[8])(__this, size); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // UnmarshalCapture - Capture the native data into marshaller instances in case conversion to managed data throws an exception. __retVal_native__marshaller.FromUnmanaged(__retVal_native); @@ -217,8 +229,11 @@ } finally { - // Cleanup - Perform required cleanup. - __retVal_native__marshaller.Free(); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__marshaller.Free(); + } } return __retVal; @@ -255,7 +270,7 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __p_native__marshaller.Free(); } @@ -294,7 +309,7 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __pIn_native__marshaller.Free(); } @@ -338,7 +353,7 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. if (__pRef_native__ownOriginal) { } @@ -381,7 +396,7 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __pOut_native__marshaller.Free(); } @@ -417,7 +432,7 @@ internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInt } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __invokeRetValUnmanaged__marshaller.Free(); } @@ -451,7 +466,7 @@ internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInt } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __retVal_native__marshaller.Free(); } diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulCollectionStatelessElement.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulCollectionStatelessElement.cs index 54a6518..68bd6fc 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulCollectionStatelessElement.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulCollectionStatelessElement.cs @@ -48,7 +48,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __p_native__nativeSpan = __p_native__marshaller.GetUnmanagedValuesDestination(); for (int __i0 = 0; __i0 < __p_native__lastIndexMarshalled; ++__i0) @@ -100,7 +100,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __pIn_native__nativeSpan = __pIn_native__marshaller.GetUnmanagedValuesDestination(); for (int __i0 = 0; __i0 < __pIn_native__lastIndexMarshalled; ++__i0) @@ -163,7 +163,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __pRef_native__nativeSpan = __pRef_native__marshaller.GetUnmanagedValuesDestination(); for (int __i0 = 0; __i0 < __pRef_native__lastIndexMarshalled; ++__i0) @@ -181,6 +181,7 @@ void global::SharedTypes.ComInterfaces.IStatefulCollectionStatelessElement.MethodOut(out global::SharedTypes.ComInterfaces.StatefulCollection pOut, out int size) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatefulCollectionStatelessElement)); + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out pOut); System.Runtime.CompilerServices.Unsafe.SkipInit(out size); byte* __pOut_native = default; @@ -197,6 +198,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[6])(__this, &__pOut_native, __size_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // UnmarshalCapture - Capture the native data into marshaller instances in case conversion to managed data throws an exception. __pOut_native__marshaller.FromUnmanaged(__pOut_native); @@ -216,16 +218,19 @@ } finally { - // Cleanup - Perform required cleanup. + if (__invokeSucceeded) { - System.ReadOnlySpan __pOut_native__nativeSpan = __pOut_native__marshaller.GetUnmanagedValuesDestination(); - for (int __i0 = 0; __i0 < __pOut_native__nativeSpan.Length; ++__i0) + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. { - global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__pOut_native__nativeSpan[__i0]); + System.ReadOnlySpan __pOut_native__nativeSpan = __pOut_native__marshaller.GetUnmanagedValuesDestination(); + for (int __i0 = 0; __i0 < __pOut_native__nativeSpan.Length; ++__i0) + { + global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__pOut_native__nativeSpan[__i0]); + } } - } - __pOut_native__marshaller.Free(); + __pOut_native__marshaller.Free(); + } } } @@ -234,6 +239,7 @@ global::SharedTypes.ComInterfaces.StatefulCollection global::SharedTypes.ComInterfaces.IStatefulCollectionStatelessElement.Return(int size) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatefulCollectionStatelessElement)); + bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.StatefulCollection __retVal = default; byte* __retVal_native = default; int __invokeRetVal = default; @@ -247,6 +253,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[7])(__this, size, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // UnmarshalCapture - Capture the native data into marshaller instances in case conversion to managed data throws an exception. __retVal_native__marshaller.FromUnmanaged(__retVal_native); @@ -266,16 +273,19 @@ } finally { - // Cleanup - Perform required cleanup. + if (__invokeSucceeded) { - System.ReadOnlySpan __retVal_native__nativeSpan = __retVal_native__marshaller.GetUnmanagedValuesDestination(); - for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. { - global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__retVal_native__nativeSpan[__i0]); + System.ReadOnlySpan __retVal_native__nativeSpan = __retVal_native__marshaller.GetUnmanagedValuesDestination(); + for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) + { + global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__retVal_native__nativeSpan[__i0]); + } } - } - __retVal_native__marshaller.Free(); + __retVal_native__marshaller.Free(); + } } return __retVal; @@ -286,6 +296,7 @@ global::SharedTypes.ComInterfaces.StatefulCollection global::SharedTypes.ComInterfaces.IStatefulCollectionStatelessElement.ReturnPreserveSig(int size) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatefulCollectionStatelessElement)); + bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.StatefulCollection __retVal = default; byte* __retVal_native = default; // Setup - Perform required setup. @@ -298,6 +309,7 @@ __retVal_native = ((delegate* unmanaged[MemberFunction] )__vtable_native[8])(__this, size); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // UnmarshalCapture - Capture the native data into marshaller instances in case conversion to managed data throws an exception. __retVal_native__marshaller.FromUnmanaged(__retVal_native); @@ -316,16 +328,19 @@ } finally { - // Cleanup - Perform required cleanup. + if (__invokeSucceeded) { - System.ReadOnlySpan __retVal_native__nativeSpan = __retVal_native__marshaller.GetUnmanagedValuesDestination(); - for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. { - global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__retVal_native__nativeSpan[__i0]); + System.ReadOnlySpan __retVal_native__nativeSpan = __retVal_native__marshaller.GetUnmanagedValuesDestination(); + for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) + { + global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__retVal_native__nativeSpan[__i0]); + } } - } - __retVal_native__marshaller.Free(); + __retVal_native__marshaller.Free(); + } } return __retVal; @@ -370,7 +385,7 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __p_native__marshaller.Free(); } @@ -417,7 +432,7 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __pIn_native__marshaller.Free(); } @@ -477,7 +492,7 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. if (__pRef_native__ownOriginal) { { @@ -535,7 +550,7 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __pOut_native__marshaller.Free(); } @@ -579,7 +594,7 @@ internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInt } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __invokeRetValUnmanaged__marshaller.Free(); } @@ -621,7 +636,7 @@ internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInt } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __retVal_native__marshaller.Free(); } diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulFinallyMarshalling.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulFinallyMarshalling.cs index f68a10e..20ac203 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulFinallyMarshalling.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulFinallyMarshalling.cs @@ -36,7 +36,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __param_native__marshaller.Free(); } } @@ -66,7 +66,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __param_native__marshaller.Free(); } } @@ -101,10 +101,9 @@ { // GuaranteedUnmarshal - Convert native data to managed data even in the case of an exception during the non-cleanup phases. param = __param_native__marshaller.ToManagedFinally(); + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __param_native__marshaller.Free(); } - - // Cleanup - Perform required cleanup. - __param_native__marshaller.Free(); } } @@ -143,7 +142,7 @@ param = __param_native__marshaller.ToManagedFinally(); } - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __param_native__marshaller.Free(); } } @@ -178,10 +177,9 @@ { // GuaranteedUnmarshal - Convert native data to managed data even in the case of an exception during the non-cleanup phases. __retVal = __retVal_native__marshaller.ToManagedFinally(); + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__marshaller.Free(); } - - // Cleanup - Perform required cleanup. - __retVal_native__marshaller.Free(); } return __retVal; @@ -214,10 +212,9 @@ { // GuaranteedUnmarshal - Convert native data to managed data even in the case of an exception during the non-cleanup phases. __retVal = __retVal_native__marshaller.ToManagedFinally(); + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__marshaller.Free(); } - - // Cleanup - Perform required cleanup. - __retVal_native__marshaller.Free(); } return __retVal; @@ -257,7 +254,7 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt param = __param_native__marshaller.ToManagedFinally(); } - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __param_native__marshaller.Free(); } @@ -296,7 +293,7 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI param = __param_native__marshaller.ToManagedFinally(); } - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __param_native__marshaller.Free(); } @@ -329,7 +326,7 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __param_native__marshaller.Free(); } @@ -372,7 +369,7 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com param = __param_native__marshaller.ToManagedFinally(); } - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __param_native__marshaller.Free(); } @@ -405,7 +402,7 @@ internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInt } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __invokeRetValUnmanaged__marshaller.Free(); } @@ -436,7 +433,7 @@ internal static nint ABI_ReturnPreserveSig(System.Runtime.InteropServices.ComWra } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __retVal_native__marshaller.Free(); } diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulMarshalling.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulMarshalling.cs index 049230d..d52fabb 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulMarshalling.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulMarshalling.cs @@ -38,7 +38,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __param_native__marshaller.Free(); } } @@ -70,7 +70,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __param_native__marshaller.Free(); } } @@ -80,6 +80,7 @@ void global::SharedTypes.ComInterfaces.IStatefulMarshalling.MethodOut(out global::SharedTypes.ComInterfaces.StatefulType param) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatefulMarshalling)); + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out param); nint __param_native = default; int __invokeRetVal = default; @@ -91,6 +92,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[5])(__this, &__param_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // UnmarshalCapture - Capture the native data into marshaller instances in case conversion to managed data throws an exception. __param_native__marshaller.FromUnmanaged(__param_native); @@ -100,8 +102,11 @@ } finally { - // Cleanup - Perform required cleanup. - __param_native__marshaller.Free(); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __param_native__marshaller.Free(); + } } } @@ -135,7 +140,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __param_native__marshaller.Free(); } } @@ -145,6 +150,7 @@ global::SharedTypes.ComInterfaces.StatefulType global::SharedTypes.ComInterfaces.IStatefulMarshalling.Return() { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatefulMarshalling)); + bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.StatefulType __retVal = default; nint __retVal_native = default; int __invokeRetVal = default; @@ -156,6 +162,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[7])(__this, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // UnmarshalCapture - Capture the native data into marshaller instances in case conversion to managed data throws an exception. __retVal_native__marshaller.FromUnmanaged(__retVal_native); @@ -165,8 +172,11 @@ } finally { - // Cleanup - Perform required cleanup. - __retVal_native__marshaller.Free(); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__marshaller.Free(); + } } return __retVal; @@ -177,6 +187,7 @@ global::SharedTypes.ComInterfaces.StatefulType global::SharedTypes.ComInterfaces.IStatefulMarshalling.ReturnPreserveSig() { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatefulMarshalling)); + bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.StatefulType __retVal = default; nint __retVal_native = default; // Setup - Perform required setup. @@ -187,6 +198,7 @@ __retVal_native = ((delegate* unmanaged[MemberFunction] )__vtable_native[8])(__this); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // UnmarshalCapture - Capture the native data into marshaller instances in case conversion to managed data throws an exception. __retVal_native__marshaller.FromUnmanaged(__retVal_native); @@ -195,8 +207,11 @@ } finally { - // Cleanup - Perform required cleanup. - __retVal_native__marshaller.Free(); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__marshaller.Free(); + } } return __retVal; @@ -229,7 +244,7 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __param_native__marshaller.Free(); } @@ -261,7 +276,7 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __param_native__marshaller.Free(); } @@ -296,7 +311,7 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __param_native__marshaller.Free(); } @@ -334,7 +349,7 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __param_native__marshaller.Free(); } @@ -369,7 +384,7 @@ internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInt } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __invokeRetValUnmanaged__marshaller.Free(); } @@ -402,7 +417,7 @@ internal static nint ABI_ReturnPreserveSig(System.Runtime.InteropServices.ComWra } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __retVal_native__marshaller.Free(); } diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulPinnedMarshalling.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulPinnedMarshalling.cs index 7291d53..388895c 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulPinnedMarshalling.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulPinnedMarshalling.cs @@ -40,7 +40,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __param_native__marshaller.Free(); } } @@ -74,7 +74,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __param_native__marshaller.Free(); } } @@ -84,6 +84,7 @@ void global::SharedTypes.ComInterfaces.IStatefulPinnedMarshalling.MethodOut(out global::SharedTypes.ComInterfaces.StatefulPinnedType param) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatefulPinnedMarshalling)); + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out param); nint __param_native = default; int __invokeRetVal = default; @@ -95,6 +96,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[5])(__this, &__param_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // UnmarshalCapture - Capture the native data into marshaller instances in case conversion to managed data throws an exception. __param_native__marshaller.FromUnmanaged(__param_native); @@ -104,8 +106,11 @@ } finally { - // Cleanup - Perform required cleanup. - __param_native__marshaller.Free(); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __param_native__marshaller.Free(); + } } } @@ -137,7 +142,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __param_native__marshaller.Free(); } } @@ -147,6 +152,7 @@ global::SharedTypes.ComInterfaces.StatefulPinnedType global::SharedTypes.ComInterfaces.IStatefulPinnedMarshalling.Return() { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatefulPinnedMarshalling)); + bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.StatefulPinnedType __retVal = default; nint __retVal_native = default; int __invokeRetVal = default; @@ -158,6 +164,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[7])(__this, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // UnmarshalCapture - Capture the native data into marshaller instances in case conversion to managed data throws an exception. __retVal_native__marshaller.FromUnmanaged(__retVal_native); @@ -167,8 +174,11 @@ } finally { - // Cleanup - Perform required cleanup. - __retVal_native__marshaller.Free(); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__marshaller.Free(); + } } return __retVal; @@ -179,6 +189,7 @@ global::SharedTypes.ComInterfaces.StatefulPinnedType global::SharedTypes.ComInterfaces.IStatefulPinnedMarshalling.ReturnPreserveSig() { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatefulPinnedMarshalling)); + bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.StatefulPinnedType __retVal = default; nint __retVal_native = default; // Setup - Perform required setup. @@ -189,6 +200,7 @@ __retVal_native = ((delegate* unmanaged[MemberFunction] )__vtable_native[8])(__this); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // UnmarshalCapture - Capture the native data into marshaller instances in case conversion to managed data throws an exception. __retVal_native__marshaller.FromUnmanaged(__retVal_native); @@ -197,8 +209,11 @@ } finally { - // Cleanup - Perform required cleanup. - __retVal_native__marshaller.Free(); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__marshaller.Free(); + } } return __retVal; @@ -231,7 +246,7 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __param_native__marshaller.Free(); } @@ -263,7 +278,7 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __param_native__marshaller.Free(); } @@ -296,7 +311,7 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __param_native__marshaller.Free(); } @@ -332,7 +347,7 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __param_native__marshaller.Free(); } @@ -365,7 +380,7 @@ internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInt } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __invokeRetValUnmanaged__marshaller.Free(); } @@ -396,7 +411,7 @@ internal static nint ABI_ReturnPreserveSig(System.Runtime.InteropServices.ComWra } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __retVal_native__marshaller.Free(); } diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessCallerAllocateBufferMarshalling.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessCallerAllocateBufferMarshalling.cs index 88caed4..40d4121 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessCallerAllocateBufferMarshalling.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessCallerAllocateBufferMarshalling.cs @@ -33,7 +33,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferTypeMarshaller.Free(__param_native); } } @@ -60,7 +60,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferTypeMarshaller.Free(__param_native); } } @@ -70,6 +70,7 @@ void global::SharedTypes.ComInterfaces.IStatelessCallerAllocateBufferMarshalling.MethodOut(out global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferType param, int size) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatelessCallerAllocateBufferMarshalling)); + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out param); nint __param_native = default; int __invokeRetVal = default; @@ -79,6 +80,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[5])(__this, &__param_native, size); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -86,8 +88,11 @@ } finally { - // Cleanup - Perform required cleanup. - global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferTypeMarshaller.Free(__param_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferTypeMarshaller.Free(__param_native); + } } } @@ -113,7 +118,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferTypeMarshaller.Free(__param_native); } } @@ -123,6 +128,7 @@ global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferType global::SharedTypes.ComInterfaces.IStatelessCallerAllocateBufferMarshalling.Return() { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatelessCallerAllocateBufferMarshalling)); + bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferType __retVal = default; nint __retVal_native = default; int __invokeRetVal = default; @@ -132,6 +138,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[7])(__this, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -139,8 +146,11 @@ } finally { - // Cleanup - Perform required cleanup. - global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferTypeMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferTypeMarshaller.Free(__retVal_native); + } } return __retVal; @@ -151,6 +161,7 @@ global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferType global::SharedTypes.ComInterfaces.IStatelessCallerAllocateBufferMarshalling.ReturnPreserveSig() { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatelessCallerAllocateBufferMarshalling)); + bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferType __retVal = default; nint __retVal_native = default; try @@ -159,14 +170,18 @@ __retVal_native = ((delegate* unmanaged[MemberFunction] )__vtable_native[8])(__this); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. __retVal = global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferTypeMarshaller.ConvertToManaged(__retVal_native); } finally { - // Cleanup - Perform required cleanup. - global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferTypeMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferTypeMarshaller.Free(__retVal_native); + } } return __retVal; @@ -271,7 +286,7 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. if (__param_native__ownOriginal) { global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferTypeMarshaller.Free(__param_native__original); diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessCollectionBlittableElement.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessCollectionBlittableElement.cs index 0ad6700..e1800b8 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessCollectionBlittableElement.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessCollectionBlittableElement.cs @@ -36,7 +36,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. ; global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__p_native); } @@ -69,7 +69,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. ; global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__pIn_native); } @@ -103,7 +103,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __pRef_native__numElements = size; global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__pRef_native); } @@ -114,6 +114,7 @@ void global::SharedTypes.ComInterfaces.IStatelessCollectionBlittableElement.MethodOut(out global::SharedTypes.ComInterfaces.StatelessCollection pOut, out int size) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatelessCollectionBlittableElement)); + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out pOut); System.Runtime.CompilerServices.Unsafe.SkipInit(out size); nint __pOut_native = default; @@ -129,6 +130,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[6])(__this, &__pOut_native, __size_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -138,9 +140,12 @@ } finally { - // Cleanup - Perform required cleanup. - __pOut_native__numElements = size; - global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__pOut_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __pOut_native__numElements = size; + global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__pOut_native); + } } } @@ -149,6 +154,7 @@ global::SharedTypes.ComInterfaces.StatelessCollection global::SharedTypes.ComInterfaces.IStatelessCollectionBlittableElement.Return(int size) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatelessCollectionBlittableElement)); + bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.StatelessCollection __retVal = default; nint __retVal_native = default; int __invokeRetVal = default; @@ -161,6 +167,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[7])(__this, size, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -170,9 +177,12 @@ } finally { - // Cleanup - Perform required cleanup. - __retVal_native__numElements = size; - global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__numElements = size; + global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__retVal_native); + } } return __retVal; @@ -183,6 +193,7 @@ global::SharedTypes.ComInterfaces.StatelessCollection global::SharedTypes.ComInterfaces.IStatelessCollectionBlittableElement.ReturnPreserveSig(int size) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatelessCollectionBlittableElement)); + bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.StatelessCollection __retVal = default; nint __retVal_native = default; // Setup - Perform required setup. @@ -194,6 +205,7 @@ __retVal_native = ((delegate* unmanaged[MemberFunction] )__vtable_native[8])(__this, size); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. __retVal_native__numElements = size; @@ -202,9 +214,12 @@ } finally { - // Cleanup - Perform required cleanup. - __retVal_native__numElements = size; - global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__numElements = size; + global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__retVal_native); + } } return __retVal; @@ -303,7 +318,7 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. if (__pRef_native__ownOriginal) { __pRef_native__numElements = size; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessCollectionStatelessElement.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessCollectionStatelessElement.cs index 907d0da..ffc58f2 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessCollectionStatelessElement.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessCollectionStatelessElement.cs @@ -45,7 +45,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __p_native__nativeSpan = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetUnmanagedValuesDestination(__p_native, __p_native__numElements); for (int __i0 = 0; __i0 < __p_native__lastIndexMarshalled; ++__i0) @@ -93,7 +93,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __pIn_native__nativeSpan = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetUnmanagedValuesDestination(__pIn_native, __pIn_native__numElements); for (int __i0 = 0; __i0 < __pIn_native__lastIndexMarshalled; ++__i0) @@ -149,7 +149,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __pRef_native__nativeSpan = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetUnmanagedValuesDestination(__pRef_native, __pRef_native__numElements); for (int __i0 = 0; __i0 < __pRef_native__lastIndexMarshalled; ++__i0) @@ -168,6 +168,7 @@ void global::SharedTypes.ComInterfaces.IStatelessCollectionStatelessElement.MethodOut(out global::SharedTypes.ComInterfaces.StatelessCollection pOut, out int size) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatelessCollectionStatelessElement)); + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out pOut); System.Runtime.CompilerServices.Unsafe.SkipInit(out size); nint __pOut_native = default; @@ -183,6 +184,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[6])(__this, &__pOut_native, __size_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -199,17 +201,20 @@ } finally { - // Cleanup - Perform required cleanup. + if (__invokeSucceeded) { - System.ReadOnlySpan __pOut_native__nativeSpan = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetUnmanagedValuesDestination(__pOut_native, __pOut_native__numElements); - for (int __i0 = 0; __i0 < __pOut_native__nativeSpan.Length; ++__i0) + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. { - global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__pOut_native__nativeSpan[__i0]); + System.ReadOnlySpan __pOut_native__nativeSpan = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetUnmanagedValuesDestination(__pOut_native, __pOut_native__numElements); + for (int __i0 = 0; __i0 < __pOut_native__nativeSpan.Length; ++__i0) + { + global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__pOut_native__nativeSpan[__i0]); + } } - } - __pOut_native__numElements = size; - global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__pOut_native); + __pOut_native__numElements = size; + global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__pOut_native); + } } } @@ -218,6 +223,7 @@ global::SharedTypes.ComInterfaces.StatelessCollection global::SharedTypes.ComInterfaces.IStatelessCollectionStatelessElement.Return(int size) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatelessCollectionStatelessElement)); + bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.StatelessCollection __retVal = default; nint __retVal_native = default; int __invokeRetVal = default; @@ -230,6 +236,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[7])(__this, size, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -246,17 +253,20 @@ } finally { - // Cleanup - Perform required cleanup. + if (__invokeSucceeded) { - System.ReadOnlySpan __retVal_native__nativeSpan = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetUnmanagedValuesDestination(__retVal_native, __retVal_native__numElements); - for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. { - global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__retVal_native__nativeSpan[__i0]); + System.ReadOnlySpan __retVal_native__nativeSpan = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetUnmanagedValuesDestination(__retVal_native, __retVal_native__numElements); + for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) + { + global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__retVal_native__nativeSpan[__i0]); + } } - } - __retVal_native__numElements = size; - global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__retVal_native); + __retVal_native__numElements = size; + global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__retVal_native); + } } return __retVal; @@ -267,6 +277,7 @@ global::SharedTypes.ComInterfaces.StatelessCollection global::SharedTypes.ComInterfaces.IStatelessCollectionStatelessElement.ReturnPreserveSig(int size) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatelessCollectionStatelessElement)); + bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.StatelessCollection __retVal = default; nint __retVal_native = default; // Setup - Perform required setup. @@ -278,6 +289,7 @@ __retVal_native = ((delegate* unmanaged[MemberFunction] )__vtable_native[8])(__this, size); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. __retVal_native__numElements = size; @@ -293,17 +305,20 @@ } finally { - // Cleanup - Perform required cleanup. + if (__invokeSucceeded) { - System.ReadOnlySpan __retVal_native__nativeSpan = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetUnmanagedValuesDestination(__retVal_native, __retVal_native__numElements); - for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. { - global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__retVal_native__nativeSpan[__i0]); + System.ReadOnlySpan __retVal_native__nativeSpan = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetUnmanagedValuesDestination(__retVal_native, __retVal_native__numElements); + for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) + { + global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__retVal_native__nativeSpan[__i0]); + } } - } - __retVal_native__numElements = size; - global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__retVal_native); + __retVal_native__numElements = size; + global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__retVal_native); + } } return __retVal; @@ -434,7 +449,7 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. if (__pRef_native__ownOriginal) { { diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessFinallyMarshalling.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessFinallyMarshalling.cs index 912a933..8828c09 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessFinallyMarshalling.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessFinallyMarshalling.cs @@ -32,7 +32,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.Free(__param_native); } } @@ -58,7 +58,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.Free(__param_native); } } @@ -89,10 +89,9 @@ { // GuaranteedUnmarshal - Convert native data to managed data even in the case of an exception during the non-cleanup phases. param = global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.ConvertToManagedFinally(__param_native); + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.Free(__param_native); } - - // Cleanup - Perform required cleanup. - global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.Free(__param_native); } } @@ -125,7 +124,7 @@ param = global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.ConvertToManagedFinally(__param_native); } - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.Free(__param_native); } } @@ -156,10 +155,9 @@ { // GuaranteedUnmarshal - Convert native data to managed data even in the case of an exception during the non-cleanup phases. __retVal = global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.ConvertToManagedFinally(__retVal_native); + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.Free(__retVal_native); } - - // Cleanup - Perform required cleanup. - global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.Free(__retVal_native); } return __retVal; @@ -188,10 +186,9 @@ { // GuaranteedUnmarshal - Convert native data to managed data even in the case of an exception during the non-cleanup phases. __retVal = global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.ConvertToManagedFinally(__retVal_native); + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.Free(__retVal_native); } - - // Cleanup - Perform required cleanup. - global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.Free(__retVal_native); } return __retVal; @@ -321,7 +318,7 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com param = global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.ConvertToManagedFinally(__param_native); } - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. if (__param_native__ownOriginal) { global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.Free(__param_native__original); diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessMarshalling.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessMarshalling.cs index ebc51bd..3525ea3 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessMarshalling.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessMarshalling.cs @@ -32,7 +32,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__param_native); } } @@ -58,7 +58,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__param_native); } } @@ -68,6 +68,7 @@ void global::SharedTypes.ComInterfaces.IStatelessMarshalling.MethodOut(out global::SharedTypes.ComInterfaces.StatelessType param, int size) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatelessMarshalling)); + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out param); nint __param_native = default; int __invokeRetVal = default; @@ -77,6 +78,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[5])(__this, &__param_native, size); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -84,8 +86,11 @@ } finally { - // Cleanup - Perform required cleanup. - global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__param_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__param_native); + } } } @@ -111,7 +116,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__param_native); } } @@ -121,6 +126,7 @@ global::SharedTypes.ComInterfaces.StatelessType global::SharedTypes.ComInterfaces.IStatelessMarshalling.Return() { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatelessMarshalling)); + bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.StatelessType __retVal = default; nint __retVal_native = default; int __invokeRetVal = default; @@ -130,6 +136,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[7])(__this, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -137,8 +144,11 @@ } finally { - // Cleanup - Perform required cleanup. - global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__retVal_native); + } } return __retVal; @@ -149,6 +159,7 @@ global::SharedTypes.ComInterfaces.StatelessType global::SharedTypes.ComInterfaces.IStatelessMarshalling.ReturnPreserveSig() { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatelessMarshalling)); + bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.StatelessType __retVal = default; nint __retVal_native = default; try @@ -157,14 +168,18 @@ __retVal_native = ((delegate* unmanaged[MemberFunction] )__vtable_native[8])(__this); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. __retVal = global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.ConvertToManaged(__retVal_native); } finally { - // Cleanup - Perform required cleanup. - global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__retVal_native); + } } return __retVal; @@ -269,7 +284,7 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. if (__param_native__ownOriginal) { global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__param_native__original); diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessPinnedMarshalling.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessPinnedMarshalling.cs index a47d487..bfc9f09 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessPinnedMarshalling.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessPinnedMarshalling.cs @@ -49,7 +49,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. global::SharedTypes.ComInterfaces.StatelessPinnedTypeMarshaller.Free(__param_native); } } @@ -59,6 +59,7 @@ void global::SharedTypes.ComInterfaces.IStatelessPinnedMarshalling.MethodOut(out global::SharedTypes.ComInterfaces.StatelessPinnedType param, int size) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatelessPinnedMarshalling)); + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out param); nint __param_native = default; int __invokeRetVal = default; @@ -68,6 +69,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[5])(__this, &__param_native, size); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -75,8 +77,11 @@ } finally { - // Cleanup - Perform required cleanup. - global::SharedTypes.ComInterfaces.StatelessPinnedTypeMarshaller.Free(__param_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::SharedTypes.ComInterfaces.StatelessPinnedTypeMarshaller.Free(__param_native); + } } } @@ -102,7 +107,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. global::SharedTypes.ComInterfaces.StatelessPinnedTypeMarshaller.Free(__param_native); } } @@ -112,6 +117,7 @@ global::SharedTypes.ComInterfaces.StatelessPinnedType global::SharedTypes.ComInterfaces.IStatelessPinnedMarshalling.Return() { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatelessPinnedMarshalling)); + bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.StatelessPinnedType __retVal = default; nint __retVal_native = default; int __invokeRetVal = default; @@ -121,6 +127,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[7])(__this, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -128,8 +135,11 @@ } finally { - // Cleanup - Perform required cleanup. - global::SharedTypes.ComInterfaces.StatelessPinnedTypeMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::SharedTypes.ComInterfaces.StatelessPinnedTypeMarshaller.Free(__retVal_native); + } } return __retVal; @@ -140,6 +150,7 @@ global::SharedTypes.ComInterfaces.StatelessPinnedType global::SharedTypes.ComInterfaces.IStatelessPinnedMarshalling.ReturnPreserveSig() { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatelessPinnedMarshalling)); + bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.StatelessPinnedType __retVal = default; nint __retVal_native = default; try @@ -148,14 +159,18 @@ __retVal_native = ((delegate* unmanaged[MemberFunction] )__vtable_native[8])(__this); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. __retVal = global::SharedTypes.ComInterfaces.StatelessPinnedTypeMarshaller.ConvertToManaged(__retVal_native); } finally { - // Cleanup - Perform required cleanup. - global::SharedTypes.ComInterfaces.StatelessPinnedTypeMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::SharedTypes.ComInterfaces.StatelessPinnedTypeMarshaller.Free(__retVal_native); + } } return __retVal; @@ -260,7 +275,7 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. if (__param_native__ownOriginal) { global::SharedTypes.ComInterfaces.StatelessPinnedTypeMarshaller.Free(__param_native__original); diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStringMarshallingOverride.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStringMarshallingOverride.cs index 2dc1608..a2b164e 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStringMarshallingOverride.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStringMarshallingOverride.cs @@ -16,6 +16,7 @@ string global::SharedTypes.ComInterfaces.IStringMarshallingOverride.StringMarshallingUtf8(string input) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStringMarshallingOverride)); + bool __invokeSucceeded = default; byte* __input_native = default; string __retVal = default; byte* __retVal_native = default; @@ -32,6 +33,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[3])(__this, __input_native, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -39,8 +41,13 @@ } finally { - // Cleanup - Perform required cleanup. - global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.Free(__retVal_native); + } + + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __input_native__marshaller.Free(); } @@ -52,6 +59,7 @@ string global::SharedTypes.ComInterfaces.IStringMarshallingOverride.MarshalAsLPWString(string input) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStringMarshallingOverride)); + bool __invokeSucceeded = default; string __retVal = default; ushort* __retVal_native = default; int __invokeRetVal = default; @@ -63,6 +71,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[4])(__this, (ushort*)__input_native, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -70,8 +79,11 @@ } finally { - // Cleanup - Perform required cleanup. - global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__retVal_native); + } } return __retVal; @@ -82,6 +94,7 @@ string global::SharedTypes.ComInterfaces.IStringMarshallingOverride.MarshalUsingUtf16(string input) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStringMarshallingOverride)); + bool __invokeSucceeded = default; string __retVal = default; ushort* __retVal_native = default; int __invokeRetVal = default; @@ -93,6 +106,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[5])(__this, (ushort*)__input_native, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -100,8 +114,11 @@ } finally { - // Cleanup - Perform required cleanup. - global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__retVal_native); + } } return __retVal; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived.cs index ce333fc..f922342 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived.cs @@ -16,6 +16,7 @@ string global::SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived.StringMarshallingUtf8_2(string input) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived)); + bool __invokeSucceeded = default; byte* __input_native = default; string __retVal = default; byte* __retVal_native = default; @@ -32,6 +33,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[6])(__this, __input_native, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -39,8 +41,13 @@ } finally { - // Cleanup - Perform required cleanup. - global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.Free(__retVal_native); + } + + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __input_native__marshaller.Free(); } @@ -52,6 +59,7 @@ string global::SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived.MarshalAsLPWString_2(string input) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived)); + bool __invokeSucceeded = default; string __retVal = default; ushort* __retVal_native = default; int __invokeRetVal = default; @@ -63,6 +71,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[7])(__this, (ushort*)__input_native, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -70,8 +79,11 @@ } finally { - // Cleanup - Perform required cleanup. - global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__retVal_native); + } } return __retVal; @@ -82,6 +94,7 @@ string global::SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived.MarshalUsingUtf16_2(string input) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived)); + bool __invokeSucceeded = default; string __retVal = default; ushort* __retVal_native = default; int __invokeRetVal = default; @@ -93,6 +106,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[8])(__this, (ushort*)__input_native, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -100,8 +114,11 @@ } finally { - // Cleanup - Perform required cleanup. - global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__retVal_native); + } } return __retVal; @@ -112,6 +129,7 @@ string global::SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived.StringMarshallingUtf8(string input) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived)); + bool __invokeSucceeded = default; byte* __input_native = default; string __retVal = default; byte* __retVal_native = default; @@ -128,6 +146,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[3])(__this, __input_native, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -135,8 +154,13 @@ } finally { - // Cleanup - Perform required cleanup. - global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.Free(__retVal_native); + } + + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __input_native__marshaller.Free(); } @@ -148,6 +172,7 @@ string global::SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived.MarshalAsLPWString(string input) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived)); + bool __invokeSucceeded = default; string __retVal = default; ushort* __retVal_native = default; int __invokeRetVal = default; @@ -159,6 +184,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[4])(__this, (ushort*)__input_native, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -166,8 +192,11 @@ } finally { - // Cleanup - Perform required cleanup. - global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__retVal_native); + } } return __retVal; @@ -178,6 +207,7 @@ string global::SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived.MarshalUsingUtf16(string input) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived)); + bool __invokeSucceeded = default; string __retVal = default; ushort* __retVal_native = default; int __invokeRetVal = default; @@ -189,6 +219,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[5])(__this, (ushort*)__input_native, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -196,8 +227,11 @@ } finally { - // Cleanup - Perform required cleanup. - global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__retVal_native); + } } return __retVal; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IUTF16Marshalling.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IUTF16Marshalling.cs index 6386ee3..d7c5399 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IUTF16Marshalling.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IUTF16Marshalling.cs @@ -16,6 +16,7 @@ string global::SharedTypes.ComInterfaces.IUTF16Marshalling.GetString() { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IUTF16Marshalling)); + bool __invokeSucceeded = default; string __retVal = default; ushort* __retVal_native = default; int __invokeRetVal = default; @@ -25,6 +26,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[3])(__this, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -32,8 +34,11 @@ } finally { - // Cleanup - Perform required cleanup. - global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__retVal_native); + } } return __retVal; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IUTF8Marshalling.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IUTF8Marshalling.cs index a7ad88a..a0281ee 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IUTF8Marshalling.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IUTF8Marshalling.cs @@ -16,6 +16,7 @@ string global::SharedTypes.ComInterfaces.IUTF8Marshalling.GetString() { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IUTF8Marshalling)); + bool __invokeSucceeded = default; string __retVal = default; byte* __retVal_native = default; int __invokeRetVal = default; @@ -25,6 +26,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[3])(__this, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -32,8 +34,11 @@ } finally { - // Cleanup - Perform required cleanup. - global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.Free(__retVal_native); + } } return __retVal; @@ -64,7 +69,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __value_native__marshaller.Free(); } } diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.MarshallingFails.ICollectionMarshallingFails.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.MarshallingFails.ICollectionMarshallingFails.cs index 7712c35..76a780d 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.MarshallingFails.ICollectionMarshallingFails.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.MarshallingFails.ICollectionMarshallingFails.cs @@ -11,11 +11,66 @@ [System.Runtime.InteropServices.DynamicInterfaceCastableImplementationAttribute] file unsafe partial interface InterfaceImplementation : global::SharedTypes.ComInterfaces.MarshallingFails.ICollectionMarshallingFails { + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.ComInterfaceGenerator", "42.42.42.42")] + [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] + int[] global::SharedTypes.ComInterfaces.MarshallingFails.ICollectionMarshallingFails.GetConstSize() + { + var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.MarshallingFails.ICollectionMarshallingFails)); + bool __invokeSucceeded = default; + int[] __retVal = default; + nint* __retVal_native = default; + int __invokeRetVal = default; + // Setup - Perform required setup. + int __retVal_native__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__numElements); + try + { + { + __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[3])(__this, &__retVal_native); + } + + __invokeSucceeded = true; + System.GC.KeepAlive(this); + // Unmarshal - Convert native data to managed data. + System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); + __retVal_native__numElements = 10; + __retVal = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__retVal_native, __retVal_native__numElements); + { + System.ReadOnlySpan __retVal_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__retVal_native, __retVal_native__numElements); + System.Span __retVal_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(__retVal); + for (int __i0 = 0; __i0 < __retVal_native__numElements; ++__i0) + { + __retVal_native__managedSpan[__i0] = global::SharedTypes.ComInterfaces.MarshallingFails.ThrowOn4thElementMarshalled.ConvertToManaged(__retVal_native__nativeSpan[__i0]); + } + } + } + finally + { + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + { + System.ReadOnlySpan __retVal_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__retVal_native, __retVal_native__numElements); + for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) + { + global::SharedTypes.ComInterfaces.MarshallingFails.ThrowOn4thElementMarshalled.Free(__retVal_native__nativeSpan[__i0]); + } + } + + __retVal_native__numElements = 10; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); + } + } + + return __retVal; + } + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.ComInterfaceGenerator", "42.42.42.42")] [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] int[] global::SharedTypes.ComInterfaces.MarshallingFails.ICollectionMarshallingFails.Get(out int size) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.MarshallingFails.ICollectionMarshallingFails)); + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out size); int[] __retVal = default; nint* __retVal_native = default; @@ -28,9 +83,10 @@ // Pin - Pin data in preparation for calling the P/Invoke. fixed (int* __size_native = &size) { - __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[3])(__this, __size_native, &__retVal_native); + __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[4])(__this, __size_native, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -47,17 +103,20 @@ } finally { - // Cleanup - Perform required cleanup. + if (__invokeSucceeded) { - System.ReadOnlySpan __retVal_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__retVal_native, __retVal_native__numElements); - for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. { - global::SharedTypes.ComInterfaces.MarshallingFails.ThrowOn4thElementMarshalled.Free(__retVal_native__nativeSpan[__i0]); + System.ReadOnlySpan __retVal_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__retVal_native, __retVal_native__numElements); + for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) + { + global::SharedTypes.ComInterfaces.MarshallingFails.ThrowOn4thElementMarshalled.Free(__retVal_native__nativeSpan[__i0]); + } } - } - __retVal_native__numElements = size; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); + __retVal_native__numElements = size; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); + } } return __retVal; @@ -93,7 +152,7 @@ { // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. __value_native = __value_native__marshaller.ToUnmanaged(); - __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[4])(__this, __value_native, size); + __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[5])(__this, __value_native, size); } System.GC.KeepAlive(this); @@ -102,7 +161,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __value_native__nativeSpan = __value_native__marshaller.GetUnmanagedValuesDestination(); for (int __i0 = 0; __i0 < __value_native__lastIndexMarshalled; ++__i0) @@ -118,6 +177,42 @@ file unsafe partial interface InterfaceImplementation { + [System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvMemberFunction) })] + internal static int ABI_GetConstSize(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint** __invokeRetValUnmanaged__param) + { + global::SharedTypes.ComInterfaces.MarshallingFails.ICollectionMarshallingFails @this = default; + ref nint* __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + int[] __invokeRetVal = default; + int __retVal = default; + // Setup - Perform required setup. + int __invokeRetValUnmanaged__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __invokeRetValUnmanaged__numElements); + try + { + // Unmarshal - Convert native data to managed data. + __retVal = 0; // S_OK + @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); + __invokeRetVal = @this.GetConstSize(); + // Marshal - Convert managed data to native data. + __invokeRetValUnmanaged = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(__invokeRetVal, out __invokeRetValUnmanaged__numElements); + { + System.ReadOnlySpan __invokeRetValUnmanaged__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(__invokeRetVal); + System.Span __invokeRetValUnmanaged__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__invokeRetValUnmanaged, __invokeRetValUnmanaged__numElements); + __invokeRetValUnmanaged__nativeSpan.Clear(); + for (int __i0 = 0; __i0 < __invokeRetValUnmanaged__managedSpan.Length; ++__i0) + { + __invokeRetValUnmanaged__nativeSpan[__i0] = global::SharedTypes.ComInterfaces.MarshallingFails.ThrowOn4thElementMarshalled.ConvertToUnmanaged(__invokeRetValUnmanaged__managedSpan[__i0]); + } + } + } + catch (System.Exception __exception) + { + __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + } + + return __retVal; + } + [System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvMemberFunction) })] internal static int ABI_Get(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, int* __size_native__param, nint** __invokeRetValUnmanaged__param) { @@ -197,7 +292,7 @@ file unsafe partial interface InterfaceImplementation { internal static void** CreateManagedVirtualFunctionTable() { - void** vtable = (void**)System.Runtime.CompilerServices.RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(global::SharedTypes.ComInterfaces.MarshallingFails.ICollectionMarshallingFails), sizeof(void*) * 5); + void** vtable = (void**)System.Runtime.CompilerServices.RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(global::SharedTypes.ComInterfaces.MarshallingFails.ICollectionMarshallingFails), sizeof(void*) * 6); { nint v0, v1, v2; System.Runtime.InteropServices.ComWrappers.GetIUnknownImpl(out v0, out v1, out v2); @@ -207,8 +302,9 @@ file unsafe partial interface InterfaceImplementation } { - vtable[3] = (void*)(delegate* unmanaged[MemberFunction] )&ABI_Get; - vtable[4] = (void*)(delegate* unmanaged[MemberFunction] )&ABI_Set; + vtable[3] = (void*)(delegate* unmanaged[MemberFunction] )&ABI_GetConstSize; + vtable[4] = (void*)(delegate* unmanaged[MemberFunction] )&ABI_Get; + vtable[5] = (void*)(delegate* unmanaged[MemberFunction] )&ABI_Set; } return vtable; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.MarshallingFails.IJaggedIntArrayMarshallingFails.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.MarshallingFails.IJaggedIntArrayMarshallingFails.cs index dad9839..d96278f 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.MarshallingFails.IJaggedIntArrayMarshallingFails.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.MarshallingFails.IJaggedIntArrayMarshallingFails.cs @@ -16,6 +16,7 @@ int[][] global::SharedTypes.ComInterfaces.MarshallingFails.IJaggedIntArrayMarshallingFails.Get(out int[] widths, out int length) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.MarshallingFails.IJaggedIntArrayMarshallingFails)); + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out widths); System.Runtime.CompilerServices.Unsafe.SkipInit(out length); int* __widths_native = default; @@ -35,6 +36,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[3])(__this, &__widths_native, __length_native, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -65,31 +67,34 @@ } finally { - // Cleanup - Perform required cleanup. - __widths_native__numElements = length; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__widths_native); + if (__invokeSucceeded) { - System.ReadOnlySpan __retVal_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__retVal_native, __retVal_native__numElements); - for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __widths_native__numElements = length; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__widths_native); { - int __retVal_native__nativeSpan____i0__numElements; - System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__nativeSpan____i0__numElements); - __retVal_native__nativeSpan____i0__numElements = widths[__i0]; + System.ReadOnlySpan __retVal_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__retVal_native, __retVal_native__numElements); + for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) { - System.ReadOnlySpan __retVal_native__nativeSpan____i0__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination((nint*)__retVal_native__nativeSpan[__i0], __retVal_native__nativeSpan____i0__numElements); - for (int __i1 = 0; __i1 < __retVal_native__nativeSpan____i0__nativeSpan.Length; ++__i1) + int __retVal_native__nativeSpan____i0__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__nativeSpan____i0__numElements); + __retVal_native__nativeSpan____i0__numElements = widths[__i0]; { - global::SharedTypes.ComInterfaces.MarshallingFails.ThrowOn4thElementMarshalled.Free(__retVal_native__nativeSpan____i0__nativeSpan[__i1]); + System.ReadOnlySpan __retVal_native__nativeSpan____i0__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination((nint*)__retVal_native__nativeSpan[__i0], __retVal_native__nativeSpan____i0__numElements); + for (int __i1 = 0; __i1 < __retVal_native__nativeSpan____i0__nativeSpan.Length; ++__i1) + { + global::SharedTypes.ComInterfaces.MarshallingFails.ThrowOn4thElementMarshalled.Free(__retVal_native__nativeSpan____i0__nativeSpan[__i1]); + } } - } - __retVal_native__nativeSpan____i0__numElements = widths[__i0]; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__retVal_native__nativeSpan[__i0]); + __retVal_native__nativeSpan____i0__numElements = widths[__i0]; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__retVal_native__nativeSpan[__i0]); + } } - } - __retVal_native__numElements = length; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); + __retVal_native__numElements = length; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); + } } return __retVal; @@ -100,6 +105,7 @@ int global::SharedTypes.ComInterfaces.MarshallingFails.IJaggedIntArrayMarshallingFails.Get2(out int[][] array, out int[] widths) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.MarshallingFails.IJaggedIntArrayMarshallingFails)); + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out array); System.Runtime.CompilerServices.Unsafe.SkipInit(out widths); System.IntPtr* __array_native = default; @@ -117,6 +123,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[4])(__this, &__array_native, &__widths_native, &__retVal); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -147,31 +154,111 @@ } finally { - // Cleanup - Perform required cleanup. - __widths_native__numElements = __retVal; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__widths_native); + if (__invokeSucceeded) { - System.ReadOnlySpan __array_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__array_native, __array_native__numElements); - for (int __i0 = 0; __i0 < __array_native__nativeSpan.Length; ++__i0) + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __widths_native__numElements = __retVal; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__widths_native); { - int __array_native__nativeSpan____i0__numElements; - System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); - __array_native__nativeSpan____i0__numElements = widths[__i0]; + System.ReadOnlySpan __array_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__array_native, __array_native__numElements); + for (int __i0 = 0; __i0 < __array_native__nativeSpan.Length; ++__i0) { - System.ReadOnlySpan __array_native__nativeSpan____i0__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination((nint*)__array_native__nativeSpan[__i0], __array_native__nativeSpan____i0__numElements); - for (int __i1 = 0; __i1 < __array_native__nativeSpan____i0__nativeSpan.Length; ++__i1) + int __array_native__nativeSpan____i0__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); + __array_native__nativeSpan____i0__numElements = widths[__i0]; { - global::SharedTypes.ComInterfaces.MarshallingFails.ThrowOn4thElementMarshalled.Free(__array_native__nativeSpan____i0__nativeSpan[__i1]); + System.ReadOnlySpan __array_native__nativeSpan____i0__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination((nint*)__array_native__nativeSpan[__i0], __array_native__nativeSpan____i0__numElements); + for (int __i1 = 0; __i1 < __array_native__nativeSpan____i0__nativeSpan.Length; ++__i1) + { + global::SharedTypes.ComInterfaces.MarshallingFails.ThrowOn4thElementMarshalled.Free(__array_native__nativeSpan____i0__nativeSpan[__i1]); + } } + + __array_native__nativeSpan____i0__numElements = widths[__i0]; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__array_native__nativeSpan[__i0]); } + } - __array_native__nativeSpan____i0__numElements = widths[__i0]; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__array_native__nativeSpan[__i0]); + __array_native__numElements = __retVal; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__array_native); + } + } + + return __retVal; + } + + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.ComInterfaceGenerator", "42.42.42.42")] + [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] + int[][] global::SharedTypes.ComInterfaces.MarshallingFails.IJaggedIntArrayMarshallingFails.GetConstSize() + { + var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.MarshallingFails.IJaggedIntArrayMarshallingFails)); + bool __invokeSucceeded = default; + int[][] __retVal = default; + System.IntPtr* __retVal_native = default; + int __invokeRetVal = default; + // Setup - Perform required setup. + int __retVal_native__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__numElements); + try + { + { + __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[5])(__this, &__retVal_native); + } + + __invokeSucceeded = true; + System.GC.KeepAlive(this); + // Unmarshal - Convert native data to managed data. + System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); + __retVal_native__numElements = 10; + __retVal = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__retVal_native, __retVal_native__numElements); + { + System.ReadOnlySpan __retVal_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__retVal_native, __retVal_native__numElements); + System.Span __retVal_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(__retVal); + for (int __i0 = 0; __i0 < __retVal_native__numElements; ++__i0) + { + int __retVal_native__nativeSpan____i0__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__nativeSpan____i0__numElements); + __retVal_native__nativeSpan____i0__numElements = 10; + __retVal_native__managedSpan[__i0] = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements((nint*)__retVal_native__nativeSpan[__i0], __retVal_native__nativeSpan____i0__numElements); + { + System.ReadOnlySpan __retVal_native__nativeSpan____i0__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource((nint*)__retVal_native__nativeSpan[__i0], __retVal_native__nativeSpan____i0__numElements); + System.Span __retVal_native__nativeSpan____i0__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(__retVal_native__managedSpan[__i0]); + for (int __i1 = 0; __i1 < __retVal_native__nativeSpan____i0__numElements; ++__i1) + { + __retVal_native__nativeSpan____i0__managedSpan[__i1] = global::SharedTypes.ComInterfaces.MarshallingFails.ThrowOn4thElementMarshalled.ConvertToManaged(__retVal_native__nativeSpan____i0__nativeSpan[__i1]); + } + } } } + } + finally + { + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + { + System.ReadOnlySpan __retVal_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__retVal_native, __retVal_native__numElements); + for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) + { + int __retVal_native__nativeSpan____i0__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__nativeSpan____i0__numElements); + __retVal_native__nativeSpan____i0__numElements = 10; + { + System.ReadOnlySpan __retVal_native__nativeSpan____i0__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination((nint*)__retVal_native__nativeSpan[__i0], __retVal_native__nativeSpan____i0__numElements); + for (int __i1 = 0; __i1 < __retVal_native__nativeSpan____i0__nativeSpan.Length; ++__i1) + { + global::SharedTypes.ComInterfaces.MarshallingFails.ThrowOn4thElementMarshalled.Free(__retVal_native__nativeSpan____i0__nativeSpan[__i1]); + } + } - __array_native__numElements = __retVal; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__array_native); + __retVal_native__nativeSpan____i0__numElements = 10; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__retVal_native__nativeSpan[__i0]); + } + } + + __retVal_native__numElements = 10; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); + } } return __retVal; @@ -219,7 +306,7 @@ { // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. __array_native = __array_native__marshaller.ToUnmanaged(); - __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[5])(__this, __array_native, (int*)__widths_native, length); + __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[6])(__this, __array_native, (int*)__widths_native, length); } System.GC.KeepAlive(this); @@ -228,7 +315,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __array_native__nativeSpan = __array_native__marshaller.GetUnmanagedValuesDestination(); for (int __i0 = 0; __i0 < __array_native__lastIndexMarshalled; ++__i0) @@ -366,6 +453,53 @@ internal static int ABI_Get2(System.Runtime.InteropServices.ComWrappers.ComInter return __retVal; } + [System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvMemberFunction) })] + internal static int ABI_GetConstSize(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, System.IntPtr** __invokeRetValUnmanaged__param) + { + global::SharedTypes.ComInterfaces.MarshallingFails.IJaggedIntArrayMarshallingFails @this = default; + ref System.IntPtr* __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + int[][] __invokeRetVal = default; + int __retVal = default; + // Setup - Perform required setup. + int __invokeRetValUnmanaged__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __invokeRetValUnmanaged__numElements); + try + { + // Unmarshal - Convert native data to managed data. + __retVal = 0; // S_OK + @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); + __invokeRetVal = @this.GetConstSize(); + // Marshal - Convert managed data to native data. + __invokeRetValUnmanaged = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(__invokeRetVal, out __invokeRetValUnmanaged__numElements); + { + System.ReadOnlySpan __invokeRetValUnmanaged__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(__invokeRetVal); + System.Span __invokeRetValUnmanaged__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__invokeRetValUnmanaged, __invokeRetValUnmanaged__numElements); + __invokeRetValUnmanaged__nativeSpan.Clear(); + for (int __i0 = 0; __i0 < __invokeRetValUnmanaged__managedSpan.Length; ++__i0) + { + int __invokeRetValUnmanaged__nativeSpan____i0__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __invokeRetValUnmanaged__nativeSpan____i0__numElements); + __invokeRetValUnmanaged__nativeSpan[__i0] = (System.IntPtr)global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(__invokeRetValUnmanaged__managedSpan[__i0], out __invokeRetValUnmanaged__nativeSpan____i0__numElements); + { + System.ReadOnlySpan __invokeRetValUnmanaged__nativeSpan____i0__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(__invokeRetValUnmanaged__managedSpan[__i0]); + System.Span __invokeRetValUnmanaged__nativeSpan____i0__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination((nint*)__invokeRetValUnmanaged__nativeSpan[__i0], __invokeRetValUnmanaged__nativeSpan____i0__numElements); + __invokeRetValUnmanaged__nativeSpan____i0__nativeSpan.Clear(); + for (int __i1 = 0; __i1 < __invokeRetValUnmanaged__nativeSpan____i0__managedSpan.Length; ++__i1) + { + __invokeRetValUnmanaged__nativeSpan____i0__nativeSpan[__i1] = global::SharedTypes.ComInterfaces.MarshallingFails.ThrowOn4thElementMarshalled.ConvertToUnmanaged(__invokeRetValUnmanaged__nativeSpan____i0__managedSpan[__i1]); + } + } + } + } + } + catch (System.Exception __exception) + { + __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + } + + return __retVal; + } + [System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvMemberFunction) })] internal static int ABI_Set(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, System.IntPtr* __array_native, int* __widths_native, int length) { @@ -423,7 +557,7 @@ file unsafe partial interface InterfaceImplementation { internal static void** CreateManagedVirtualFunctionTable() { - void** vtable = (void**)System.Runtime.CompilerServices.RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(global::SharedTypes.ComInterfaces.MarshallingFails.IJaggedIntArrayMarshallingFails), sizeof(void*) * 6); + void** vtable = (void**)System.Runtime.CompilerServices.RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(global::SharedTypes.ComInterfaces.MarshallingFails.IJaggedIntArrayMarshallingFails), sizeof(void*) * 7); { nint v0, v1, v2; System.Runtime.InteropServices.ComWrappers.GetIUnknownImpl(out v0, out v1, out v2); @@ -435,7 +569,8 @@ file unsafe partial interface InterfaceImplementation { vtable[3] = (void*)(delegate* unmanaged[MemberFunction] )&ABI_Get; vtable[4] = (void*)(delegate* unmanaged[MemberFunction] )&ABI_Get2; - vtable[5] = (void*)(delegate* unmanaged[MemberFunction] )&ABI_Set; + vtable[5] = (void*)(delegate* unmanaged[MemberFunction] )&ABI_GetConstSize; + vtable[6] = (void*)(delegate* unmanaged[MemberFunction] )&ABI_Set; } return vtable; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.MarshallingFails.IStringArrayMarshallingFails.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.MarshallingFails.IStringArrayMarshallingFails.cs index 71cc4e9..ffc1c91 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.MarshallingFails.IStringArrayMarshallingFails.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.MarshallingFails.IStringArrayMarshallingFails.cs @@ -50,7 +50,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. _ = __value_native__lastIndexMarshalled; __value_native__marshaller.Free(); } @@ -95,7 +95,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. _ = __value_native__lastIndexMarshalled; __value_native__marshaller.Free(); } @@ -145,7 +145,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. _ = __value_native__lastIndexMarshalled; __value_native__numElements = 10; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__value_native); @@ -157,6 +157,7 @@ void global::SharedTypes.ComInterfaces.MarshallingFails.IStringArrayMarshallingFails.OutParam(out string[] value) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.MarshallingFails.IStringArrayMarshallingFails)); + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out value); nint* __value_native = default; int __invokeRetVal = default; @@ -169,6 +170,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[6])(__this, &__value_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -185,9 +187,12 @@ } finally { - // Cleanup - Perform required cleanup. - __value_native__numElements = 10; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__value_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __value_native__numElements = 10; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__value_native); + } } } @@ -230,7 +235,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __value_native__marshaller.Free(); } } @@ -283,7 +288,7 @@ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. _ = __value_native__lastIndexMarshalled; __value_native__marshaller.Free(); } @@ -294,6 +299,7 @@ string[] global::SharedTypes.ComInterfaces.MarshallingFails.IStringArrayMarshallingFails.ReturnValue() { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.MarshallingFails.IStringArrayMarshallingFails)); + bool __invokeSucceeded = default; string[] __retVal = default; nint* __retVal_native = default; int __invokeRetVal = default; @@ -306,6 +312,7 @@ __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[9])(__this, &__retVal_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); @@ -322,9 +329,12 @@ } finally { - // Cleanup - Perform required cleanup. - __retVal_native__numElements = 10; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__numElements = 10; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); + } } return __retVal; @@ -452,7 +462,7 @@ internal static int ABI_RefParam(System.Runtime.InteropServices.ComWrappers.ComI } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. if (__value_native__ownOriginal) { _ = __value_native__lastIndexMarshalled; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/ManagedToNativeStubs.g.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/ManagedToNativeStubs.g.cs index cd107bd..4cf5ac2 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/ManagedToNativeStubs.g.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/ManagedToNativeStubs.g.cs @@ -150,7 +150,7 @@ internal unsafe partial interface Native } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __values_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__values_native); } @@ -259,6 +259,7 @@ internal unsafe partial interface Native global::SharedTypes.IntWrapper global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObject.GetData() { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObject)); + bool __invokeSucceeded = default; global::SharedTypes.IntWrapper __retVal = default; int __retVal_native = default; try @@ -267,14 +268,18 @@ internal unsafe partial interface Native __retVal_native = ((delegate* unmanaged )__vtable_native[0])(__this); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. __retVal = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToManaged(__retVal_native); } finally { - // Cleanup - Perform required cleanup. - global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__retVal_native); + } } return __retVal; @@ -312,7 +317,7 @@ internal unsafe partial interface Native } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__x_native); } } @@ -351,7 +356,7 @@ internal unsafe partial interface Native } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__data_native); } } @@ -375,6 +380,7 @@ internal unsafe partial interface Native void global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObject.SumAndSetData(global::SharedTypes.IntWrapper[] values, int numValues, out global::SharedTypes.IntWrapper oldValue) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObject)); + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out oldValue); int* __values_native = default; int __oldValue_native = default; @@ -404,14 +410,20 @@ internal unsafe partial interface Native ((delegate* unmanaged )__vtable_native[3])(__this, __values_native, numValues, &__oldValue_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. oldValue = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToManaged(__oldValue_native); } finally { - // Cleanup - Perform required cleanup. - global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__oldValue_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__oldValue_native); + } + + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __values_native__nativeSpan = __values_native__marshaller.GetUnmanagedValuesDestination(); for (int __i0 = 0; __i0 < __values_native__lastIndexMarshalled; ++__i0) @@ -443,6 +455,7 @@ internal unsafe partial interface Native void global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObject.SumAndSetData(ref global::SharedTypes.IntWrapper[] values, int numValues, out global::SharedTypes.IntWrapper oldValue) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObject)); + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out oldValue); int* __values_native = default; int __oldValue_native = default; @@ -467,6 +480,7 @@ internal unsafe partial interface Native ((delegate* unmanaged )__vtable_native[4])(__this, &__values_native, numValues, &__oldValue_native); } + __invokeSucceeded = true; System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. oldValue = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToManaged(__oldValue_native); @@ -483,8 +497,13 @@ internal unsafe partial interface Native } finally { - // Cleanup - Perform required cleanup. - global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__oldValue_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__oldValue_native); + } + + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __values_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__values_native, __values_native__numElements); for (int __i0 = 0; __i0 < __values_native__lastIndexMarshalled; ++__i0) @@ -558,7 +577,7 @@ internal unsafe partial interface Native } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __values123_native__nativeSpan = __values123_native__marshaller.GetUnmanagedValuesDestination(); for (int __i0 = 0; __i0 < __values123_native__lastIndexMarshalled; ++__i0) diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/NativeToManagedStubs.g.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/NativeToManagedStubs.g.cs index 17b9691..087c9fd 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/NativeToManagedStubs.g.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/NativeToManagedStubs.g.cs @@ -148,7 +148,7 @@ internal static void ABI_SumAndSetData(void* __this_native, int** __values_nativ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. if (__values_native__ownOriginal) { __values_native__numElements = numValues; @@ -275,7 +275,7 @@ internal static void ABI_ExchangeData(void* __this_native, int* __data_native__p } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. if (__data_native__ownOriginal) { global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__data_native__original); @@ -384,7 +384,7 @@ internal static void ABI_SumAndSetData(void* __this_native, int** __values_nativ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. if (__values_native__ownOriginal) { { @@ -499,7 +499,7 @@ internal static void ABI_SumAndSetData(void* __this_native, int* __values_native } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __values_native__marshaller.Free(); } } @@ -568,7 +568,7 @@ internal static void ABI_SumAndSetData(void* __this_native, int** __values_nativ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. if (__values_native__ownOriginal) { { @@ -640,7 +640,7 @@ internal static void ABI_MultiplyWithData(void* __this_native, int* __values123_ } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __values123_native__marshaller.Free(); } } diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs index fd35f4d..21ed3d7 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs @@ -34,7 +34,7 @@ public static partial void SetComObjectData(global::SharedTypes.ComInterfaces.IG } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.Free(__obj_native); } @@ -64,7 +64,7 @@ public static partial int GetComObjectData(global::SharedTypes.ComInterfaces.IGe } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.Free(__obj_native); } @@ -83,6 +83,7 @@ public unsafe partial class GeneratedComInterfaceComImportInteropTests [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] private static partial global::SharedTypes.ComInterfaces.IGetAndSetInt NewNativeObject() { + bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.IGetAndSetInt __retVal = default; void* __retVal_native = default; try @@ -91,13 +92,17 @@ public unsafe partial class GeneratedComInterfaceComImportInteropTests __retVal_native = __PInvoke(); } + __invokeSucceeded = true; // Unmarshal - Convert native data to managed data. __retVal = global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.ConvertToManaged(__retVal_native); } finally { - // Cleanup - Perform required cleanup. - global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.Free(__retVal_native); + } } return __retVal; @@ -131,6 +136,7 @@ public unsafe partial class IGetAndSetIntTests [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] internal static partial global::SharedTypes.ComInterfaces.IGetAndSetInt NewNativeObjectWithMarshaller() { + bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.IGetAndSetInt __retVal = default; void* __retVal_native = default; try @@ -139,13 +145,17 @@ public unsafe partial class IGetAndSetIntTests __retVal_native = __PInvoke(); } + __invokeSucceeded = true; // Unmarshal - Convert native data to managed data. __retVal = global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.ConvertToManaged(__retVal_native); } finally { - // Cleanup - Perform required cleanup. - global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.Free(__retVal_native); + } } return __retVal; @@ -163,6 +173,7 @@ public unsafe partial class IGetAndSetIntTests [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] internal static partial global::SharedTypes.ComInterfaces.IGetAndSetInt NewNativeObjectWithUniqueMarshaller() { + bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.IGetAndSetInt __retVal = default; void* __retVal_native = default; try @@ -171,13 +182,17 @@ public unsafe partial class IGetAndSetIntTests __retVal_native = __PInvoke(); } + __invokeSucceeded = true; // Unmarshal - Convert native data to managed data. __retVal = global::System.Runtime.InteropServices.Marshalling.UniqueComInterfaceMarshaller.ConvertToManaged(__retVal_native); } finally { - // Cleanup - Perform required cleanup. - global::System.Runtime.InteropServices.Marshalling.UniqueComInterfaceMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::System.Runtime.InteropServices.Marshalling.UniqueComInterfaceMarshaller.Free(__retVal_native); + } } return __retVal; @@ -420,7 +435,7 @@ public static partial int SumAndSetNativeObjectData(void* obj, ref int[] arr, in } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __arr_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__arr_native); } diff --git a/LibraryImportGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs b/LibraryImportGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs index 93747d9..bd67689 100644 --- a/LibraryImportGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs +++ b/LibraryImportGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs @@ -80,7 +80,7 @@ public static partial int SumInArray(in int[] values, int numValues) } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __values_native__marshaller.Free(); } @@ -122,7 +122,7 @@ public static partial void Duplicate(ref int[] values, int numValues) } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __values_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__values_native); } @@ -144,6 +144,7 @@ public unsafe partial class Arrays [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial int[] CreateRange(int start, int end, out int numValues) { + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out numValues); int[] __retVal = default; int* __retVal_native = default; @@ -158,6 +159,7 @@ public static partial int[] CreateRange(int start, int end, out int numValues) __retVal_native = __PInvoke(start, end, __numValues_native); } + __invokeSucceeded = true; // Unmarshal - Convert native data to managed data. __retVal_native__numElements = numValues; __retVal = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__retVal_native, __retVal_native__numElements); @@ -165,9 +167,12 @@ public static partial int[] CreateRange(int start, int end, out int numValues) } finally { - // Cleanup - Perform required cleanup. - __retVal_native__numElements = numValues; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__numElements = numValues; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); + } } return __retVal; @@ -188,6 +193,7 @@ public unsafe partial class Arrays [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial void CreateRange_Out(int start, int end, out int numValues, out int[] res) { + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out numValues); System.Runtime.CompilerServices.Unsafe.SkipInit(out res); int* __res_native = default; @@ -202,6 +208,7 @@ public static partial void CreateRange_Out(int start, int end, out int numValues __PInvoke(start, end, __numValues_native, &__res_native); } + __invokeSucceeded = true; // Unmarshal - Convert native data to managed data. __res_native__numElements = numValues; res = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__res_native, __res_native__numElements); @@ -209,9 +216,12 @@ public static partial void CreateRange_Out(int start, int end, out int numValues } finally { - // Cleanup - Perform required cleanup. - __res_native__numElements = numValues; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__res_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __res_native__numElements = numValues; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__res_native); + } } // Local P/Invoke @@ -299,7 +309,7 @@ public static partial void ReverseChars(ref char[] chars, int numElements) } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __chars_native__numElements = numElements; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__chars_native); } @@ -351,7 +361,7 @@ public static partial int SumStringLengths(string[] strArray) } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __strArray_native__nativeSpan = __strArray_native__marshaller.GetUnmanagedValuesDestination(); for (int __i0 = 0; __i0 < __strArray_native__lastIndexMarshalled; ++__i0) @@ -420,7 +430,7 @@ public static partial void ReverseStrings_Ref(ref string[] strArray, out int num } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __strArray_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__strArray_native, __strArray_native__numElements); for (int __i0 = 0; __i0 < __strArray_native__lastIndexMarshalled; ++__i0) @@ -450,6 +460,7 @@ public unsafe partial class Arrays [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial string[] ReverseStrings_Return(string[] strArray, out int numElements) { + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out numElements); System.IntPtr* __strArray_native = default; string[] __retVal = default; @@ -483,6 +494,7 @@ public static partial string[] ReverseStrings_Return(string[] strArray, out int __retVal_native = __PInvoke(__strArray_native, __numElements_native); } + __invokeSucceeded = true; // Unmarshal - Convert native data to managed data. __retVal_native__numElements = numElements; __retVal = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__retVal_native, __retVal_native__numElements); @@ -497,17 +509,22 @@ public static partial string[] ReverseStrings_Return(string[] strArray, out int } finally { - // Cleanup - Perform required cleanup. + if (__invokeSucceeded) { - System.ReadOnlySpan __retVal_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__retVal_native, __retVal_native__numElements); - for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. { - global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free((ushort*)__retVal_native__nativeSpan[__i0]); + System.ReadOnlySpan __retVal_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__retVal_native, __retVal_native__numElements); + for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) + { + global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free((ushort*)__retVal_native__nativeSpan[__i0]); + } } + + __retVal_native__numElements = numElements; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); } - __retVal_native__numElements = numElements; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __strArray_native__nativeSpan = __strArray_native__marshaller.GetUnmanagedValuesDestination(); for (int __i0 = 0; __i0 < __strArray_native__lastIndexMarshalled; ++__i0) @@ -537,6 +554,7 @@ public unsafe partial class Arrays [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial void ReverseStrings_Out(string[] strArray, out int numElements, out string[] res) { + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out numElements); System.Runtime.CompilerServices.Unsafe.SkipInit(out res); System.IntPtr* __strArray_native = default; @@ -570,6 +588,7 @@ public static partial void ReverseStrings_Out(string[] strArray, out int numElem __PInvoke(__strArray_native, __numElements_native, &__res_native); } + __invokeSucceeded = true; // Unmarshal - Convert native data to managed data. __res_native__numElements = numElements; res = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__res_native, __res_native__numElements); @@ -584,17 +603,22 @@ public static partial void ReverseStrings_Out(string[] strArray, out int numElem } finally { - // Cleanup - Perform required cleanup. + if (__invokeSucceeded) { - System.ReadOnlySpan __res_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__res_native, __res_native__numElements); - for (int __i0 = 0; __i0 < __res_native__nativeSpan.Length; ++__i0) + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. { - global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free((ushort*)__res_native__nativeSpan[__i0]); + System.ReadOnlySpan __res_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__res_native, __res_native__numElements); + for (int __i0 = 0; __i0 < __res_native__nativeSpan.Length; ++__i0) + { + global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free((ushort*)__res_native__nativeSpan[__i0]); + } } + + __res_native__numElements = numElements; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__res_native); } - __res_native__numElements = numElements; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__res_native); + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __strArray_native__nativeSpan = __strArray_native__marshaller.GetUnmanagedValuesDestination(); for (int __i0 = 0; __i0 < __strArray_native__lastIndexMarshalled; ++__i0) @@ -623,6 +647,7 @@ public unsafe partial class Arrays [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial byte[] GetLongBytes(long l) { + bool __invokeSucceeded = default; byte[] __retVal = default; byte* __retVal_native = default; // Setup - Perform required setup. @@ -634,6 +659,7 @@ public static partial byte[] GetLongBytes(long l) __retVal_native = __PInvoke(l); } + __invokeSucceeded = true; // Unmarshal - Convert native data to managed data. __retVal_native__numElements = 8; __retVal = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__retVal_native, __retVal_native__numElements); @@ -641,9 +667,12 @@ public static partial byte[] GetLongBytes(long l) } finally { - // Cleanup - Perform required cleanup. - __retVal_native__numElements = 8; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__numElements = 8; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); + } } return __retVal; @@ -684,7 +713,7 @@ public static partial void Append(ref int[] values, int numOriginalValues, int n } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __values_native__numElements = checked(1 + numOriginalValues); global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__values_native); } @@ -740,7 +769,7 @@ public static partial bool FillRangeArray(global::SharedTypes.IntStructWrapper[] } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __array_native__marshaller.Free(); } @@ -802,7 +831,7 @@ public static partial void DoubleValues(global::SharedTypes.IntStructWrapper[] a } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. _ = __array_native__lastIndexMarshalled; __array_native__marshaller.Free(); } @@ -858,7 +887,7 @@ public static partial bool AndAllMembers(global::SharedTypes.BoolStruct[] pArray } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. _ = __pArray_native__lastIndexMarshalled; __pArray_native__marshaller.Free(); } @@ -915,7 +944,7 @@ public static partial bool AndAllMembersIn(in global::SharedTypes.BoolStruct[] p } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. _ = __pArray_native__lastIndexMarshalled; __pArray_native__marshaller.Free(); } @@ -974,7 +1003,7 @@ public static partial void NegateBools(ref global::SharedTypes.BoolStruct[] bool } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. _ = __boolStruct_native__lastIndexMarshalled; __boolStruct_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__boolStruct_native); @@ -997,6 +1026,7 @@ public unsafe partial class Arrays [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial void NegateBools(global::SharedTypes.BoolStruct[] boolStruct, int numValues, out global::SharedTypes.BoolStruct[] pBoolStructOut) { + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out pBoolStructOut); global::SharedTypes.BoolStructMarshaller.BoolStructNative* __boolStruct_native = default; global::SharedTypes.BoolStructMarshaller.BoolStructNative* __pBoolStructOut_native = default; @@ -1028,6 +1058,7 @@ public static partial void NegateBools(global::SharedTypes.BoolStruct[] boolStru __PInvoke(__boolStruct_native, numValues, &__pBoolStructOut_native); } + __invokeSucceeded = true; // Unmarshal - Convert native data to managed data. __pBoolStructOut_native__numElements = numValues; pBoolStructOut = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__pBoolStructOut_native, __pBoolStructOut_native__numElements); @@ -1042,9 +1073,14 @@ public static partial void NegateBools(global::SharedTypes.BoolStruct[] boolStru } finally { - // Cleanup - Perform required cleanup. - __pBoolStructOut_native__numElements = numValues; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__pBoolStructOut_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __pBoolStructOut_native__numElements = numValues; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__pBoolStructOut_native); + } + + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. _ = __boolStruct_native__lastIndexMarshalled; __boolStruct_native__marshaller.Free(); } @@ -1066,6 +1102,7 @@ public unsafe partial class Arrays [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial global::SharedTypes.BoolStruct[] NegateBools(global::SharedTypes.BoolStruct[] boolStruct, int numValues) { + bool __invokeSucceeded = default; global::SharedTypes.BoolStructMarshaller.BoolStructNative* __boolStruct_native = default; global::SharedTypes.BoolStruct[] __retVal = default; global::SharedTypes.BoolStructMarshaller.BoolStructNative* __retVal_native = default; @@ -1097,6 +1134,7 @@ public unsafe partial class Arrays __retVal_native = __PInvoke(__boolStruct_native, numValues); } + __invokeSucceeded = true; // Unmarshal - Convert native data to managed data. __retVal_native__numElements = numValues; __retVal = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__retVal_native, __retVal_native__numElements); @@ -1111,9 +1149,14 @@ public unsafe partial class Arrays } finally { - // Cleanup - Perform required cleanup. - __retVal_native__numElements = numValues; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__numElements = numValues; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); + } + + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. _ = __boolStruct_native__lastIndexMarshalled; __boolStruct_native__marshaller.Free(); } @@ -1136,6 +1179,7 @@ public unsafe partial class Arrays [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial int[][] TransposeMatrix(int[][] matrix, int[] numRows, int numColumns) { + bool __invokeSucceeded = default; System.IntPtr* __matrix_native = default; int[][] __retVal = default; System.IntPtr* __retVal_native = default; @@ -1171,6 +1215,7 @@ public static partial int[][] TransposeMatrix(int[][] matrix, int[] numRows, int __retVal_native = __PInvoke(__matrix_native, (int*)__numRows_native, numColumns); } + __invokeSucceeded = true; // Unmarshal - Convert native data to managed data. __retVal_native__numElements = numColumns; __retVal = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__retVal_native, __retVal_native__numElements); @@ -1189,20 +1234,25 @@ public static partial int[][] TransposeMatrix(int[][] matrix, int[] numRows, int } finally { - // Cleanup - Perform required cleanup. + if (__invokeSucceeded) { - System.ReadOnlySpan __retVal_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__retVal_native, __retVal_native__numElements); - for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. { - int __retVal_native__nativeSpan____i0__numElements; - System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__nativeSpan____i0__numElements); - __retVal_native__nativeSpan____i0__numElements = numRows[__i0]; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__retVal_native__nativeSpan[__i0]); + System.ReadOnlySpan __retVal_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__retVal_native, __retVal_native__numElements); + for (int __i0 = 0; __i0 < __retVal_native__nativeSpan.Length; ++__i0) + { + int __retVal_native__nativeSpan____i0__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__nativeSpan____i0__numElements); + __retVal_native__nativeSpan____i0__numElements = numRows[__i0]; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__retVal_native__nativeSpan[__i0]); + } } + + __retVal_native__numElements = numColumns; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); } - __retVal_native__numElements = numColumns; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __matrix_native__nativeSpan = __matrix_native__marshaller.GetUnmanagedValuesDestination(); for (int __i0 = 0; __i0 < __matrix_native__lastIndexMarshalled; ++__i0) @@ -1281,7 +1331,7 @@ public static unsafe partial int SumInArray(in int*[] values, int numValues) } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __values_native__marshaller.Free(); } @@ -1323,7 +1373,7 @@ public static unsafe partial void Duplicate(ref int*[] values, int numValues) } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __values_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.PointerArrayMarshaller.Free(__values_native); } @@ -1345,6 +1395,7 @@ public unsafe partial class Arrays [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static unsafe partial int*[] ReturnDuplicate(int*[] values, int numValues) { + bool __invokeSucceeded = default; int*[] __retVal = default; nint* __retVal_native = default; // Setup - Perform required setup. @@ -1358,6 +1409,7 @@ public unsafe partial class Arrays __retVal_native = __PInvoke((nint*)__values_native, numValues); } + __invokeSucceeded = true; // Unmarshal - Convert native data to managed data. __retVal_native__numElements = numValues; __retVal = global::System.Runtime.InteropServices.Marshalling.PointerArrayMarshaller.AllocateContainerForManagedElements(__retVal_native, __retVal_native__numElements); @@ -1365,9 +1417,12 @@ public unsafe partial class Arrays } finally { - // Cleanup - Perform required cleanup. - __retVal_native__numElements = numValues; - global::System.Runtime.InteropServices.Marshalling.PointerArrayMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__numElements = numValues; + global::System.Runtime.InteropServices.Marshalling.PointerArrayMarshaller.Free(__retVal_native); + } } return __retVal; @@ -2106,7 +2161,7 @@ public static partial void Utf8StringSpan(global::System.ReadOnlySpan s) } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __s_native__nativeSpan = __s_native__marshaller.GetUnmanagedValuesDestination(); for (int __i0 = 0; __i0 < __s_native__lastIndexMarshalled; ++__i0) @@ -2164,7 +2219,7 @@ public static partial void Utf8StringArray(string[] s) } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __s_native__nativeSpan = __s_native__marshaller.GetUnmanagedValuesDestination(); for (int __i0 = 0; __i0 < __s_native__lastIndexMarshalled; ++__i0) @@ -2222,7 +2277,7 @@ public static partial void MarshalSingleDimensionalArray(global::SharedTypes.Boo } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __c_native__nativeSpan = __c_native__marshaller.GetUnmanagedValuesDestination(); for (int __i0 = 0; __i0 < __c_native__lastIndexMarshalled; ++__i0) @@ -2280,7 +2335,7 @@ public static partial void MarshalMultidimensionalArray_CheckOuterArrayIsIndexTr } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __c_native__nativeSpan = __c_native__marshaller.GetUnmanagedValuesDestination(); for (int __i0 = 0; __i0 < __c_native__lastIndexMarshalled; ++__i0) @@ -2349,7 +2404,7 @@ public static partial void MarshalMultidimensionalArray_CheckInnerArraysAreClear } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __c_native__nativeSpan = __c_native__marshaller.GetUnmanagedValuesDestination(); for (int __i0 = 0; __i0 < __c_native__lastIndexMarshalled; ++__i0) @@ -2424,7 +2479,7 @@ public static partial void MarshalArray_Ref(ref global::SharedTypes.BoolStruct[] } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __c_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__c_native, __c_native__numElements); for (int __i0 = 0; __i0 < __c_native__lastIndexMarshalled; ++__i0) @@ -2454,6 +2509,7 @@ public unsafe partial class MarshallingFails [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial void NegateBoolsOut(global::SharedTypes.BoolStruct[] boolStruct, int numValues, out global::SharedTypes.BoolStruct[] pBoolStructOut) { + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out pBoolStructOut); global::SharedTypes.BoolStructMarshaller.BoolStructNative* __boolStruct_native = default; nint* __pBoolStructOut_native = default; @@ -2485,6 +2541,7 @@ public static partial void NegateBoolsOut(global::SharedTypes.BoolStruct[] boolS __PInvoke(__boolStruct_native, numValues, &__pBoolStructOut_native); } + __invokeSucceeded = true; // Unmarshal - Convert native data to managed data. __pBoolStructOut_native__numElements = numValues; pBoolStructOut = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__pBoolStructOut_native, __pBoolStructOut_native__numElements); @@ -2499,17 +2556,22 @@ public static partial void NegateBoolsOut(global::SharedTypes.BoolStruct[] boolS } finally { - // Cleanup - Perform required cleanup. + if (__invokeSucceeded) { - System.ReadOnlySpan __pBoolStructOut_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__pBoolStructOut_native, __pBoolStructOut_native__numElements); - for (int __i0 = 0; __i0 < __pBoolStructOut_native__nativeSpan.Length; ++__i0) + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. { - global::LibraryImportGenerator.IntegrationTests.EnforceAllElementsCleanedUpBoolStruct.Free(__pBoolStructOut_native__nativeSpan[__i0]); + System.ReadOnlySpan __pBoolStructOut_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__pBoolStructOut_native, __pBoolStructOut_native__numElements); + for (int __i0 = 0; __i0 < __pBoolStructOut_native__nativeSpan.Length; ++__i0) + { + global::LibraryImportGenerator.IntegrationTests.EnforceAllElementsCleanedUpBoolStruct.Free(__pBoolStructOut_native__nativeSpan[__i0]); + } } + + __pBoolStructOut_native__numElements = numValues; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__pBoolStructOut_native); } - __pBoolStructOut_native__numElements = numValues; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__pBoolStructOut_native); + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. _ = __boolStruct_native__lastIndexMarshalled; __boolStruct_native__marshaller.Free(); } @@ -2531,6 +2593,7 @@ public unsafe partial class MarshallingFails [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial void NegateBoolsOut2D(global::SharedTypes.BoolStruct[][] boolStruct, int length, int[] widths, out global::SharedTypes.BoolStruct[][] pBoolStructOut) { + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out pBoolStructOut); System.IntPtr* __boolStruct_native = default; System.IntPtr* __pBoolStructOut_native = default; @@ -2574,6 +2637,7 @@ public static partial void NegateBoolsOut2D(global::SharedTypes.BoolStruct[][] b __PInvoke(__boolStruct_native, length, (int*)__widths_native, &__pBoolStructOut_native); } + __invokeSucceeded = true; // Unmarshal - Convert native data to managed data. __pBoolStructOut_native__numElements = length; pBoolStructOut = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__pBoolStructOut_native, __pBoolStructOut_native__numElements); @@ -2599,29 +2663,34 @@ public static partial void NegateBoolsOut2D(global::SharedTypes.BoolStruct[][] b } finally { - // Cleanup - Perform required cleanup. + if (__invokeSucceeded) { - System.ReadOnlySpan __pBoolStructOut_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__pBoolStructOut_native, __pBoolStructOut_native__numElements); - for (int __i0 = 0; __i0 < __pBoolStructOut_native__nativeSpan.Length; ++__i0) + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. { - int __pBoolStructOut_native__nativeSpan____i0__numElements; - System.Runtime.CompilerServices.Unsafe.SkipInit(out __pBoolStructOut_native__nativeSpan____i0__numElements); - __pBoolStructOut_native__nativeSpan____i0__numElements = widths[__i0]; + System.ReadOnlySpan __pBoolStructOut_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__pBoolStructOut_native, __pBoolStructOut_native__numElements); + for (int __i0 = 0; __i0 < __pBoolStructOut_native__nativeSpan.Length; ++__i0) { - System.ReadOnlySpan __pBoolStructOut_native__nativeSpan____i0__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination((nint*)__pBoolStructOut_native__nativeSpan[__i0], __pBoolStructOut_native__nativeSpan____i0__numElements); - for (int __i1 = 0; __i1 < __pBoolStructOut_native__nativeSpan____i0__nativeSpan.Length; ++__i1) + int __pBoolStructOut_native__nativeSpan____i0__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __pBoolStructOut_native__nativeSpan____i0__numElements); + __pBoolStructOut_native__nativeSpan____i0__numElements = widths[__i0]; { - global::LibraryImportGenerator.IntegrationTests.EnforceAllElementsCleanedUpBoolStruct.Free(__pBoolStructOut_native__nativeSpan____i0__nativeSpan[__i1]); + System.ReadOnlySpan __pBoolStructOut_native__nativeSpan____i0__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination((nint*)__pBoolStructOut_native__nativeSpan[__i0], __pBoolStructOut_native__nativeSpan____i0__numElements); + for (int __i1 = 0; __i1 < __pBoolStructOut_native__nativeSpan____i0__nativeSpan.Length; ++__i1) + { + global::LibraryImportGenerator.IntegrationTests.EnforceAllElementsCleanedUpBoolStruct.Free(__pBoolStructOut_native__nativeSpan____i0__nativeSpan[__i1]); + } } - } - __pBoolStructOut_native__nativeSpan____i0__numElements = widths[__i0]; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__pBoolStructOut_native__nativeSpan[__i0]); + __pBoolStructOut_native__nativeSpan____i0__numElements = widths[__i0]; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__pBoolStructOut_native__nativeSpan[__i0]); + } } + + __pBoolStructOut_native__numElements = length; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__pBoolStructOut_native); } - __pBoolStructOut_native__numElements = length; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__pBoolStructOut_native); + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __boolStruct_native__nativeSpan = __boolStruct_native__marshaller.GetUnmanagedValuesDestination(); for (int __i0 = 0; __i0 < __boolStruct_native__lastIndexMarshalled; ++__i0) @@ -2687,7 +2756,7 @@ public static partial bool FillRangeArray(global::SharedTypes.IntStructWrapper[] } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __array_native__nativeSpan = __array_native__marshaller.GetUnmanagedValuesDestination(); for (int __i0 = 0; __i0 < __array_native__nativeSpan.Length; ++__i0) @@ -2763,7 +2832,7 @@ public static partial bool FillRangeArray2D(global::SharedTypes.IntStructWrapper } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __array_native__nativeSpan = __array_native__marshaller.GetUnmanagedValuesDestination(); for (int __i0 = 0; __i0 < __array_native__nativeSpan.Length; ++__i0) @@ -2841,7 +2910,7 @@ public static partial void NegateBoolsRef(ref global::SharedTypes.BoolStruct[] b } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __boolStruct_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__boolStruct_native, __boolStruct_native__numElements); for (int __i0 = 0; __i0 < __boolStruct_native__lastIndexMarshalled; ++__i0) @@ -2907,7 +2976,7 @@ public static partial void NegateBoolsRef2D_LastElementMarshalling(ref global::S } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __boolStruct_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__boolStruct_native, __boolStruct_native__numElements); for (int __i0 = 0; __i0 < __boolStruct_native__lastIndexMarshalled; ++__i0) @@ -2997,7 +3066,7 @@ public static partial void NegateBoolsRef2D_ClearMarshalling(ref global::SharedT } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __boolStruct_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__boolStruct_native, __boolStruct_native__numElements); for (int __i0 = 0; __i0 < __boolStruct_native__lastIndexMarshalled; ++__i0) @@ -3057,7 +3126,7 @@ public static partial int Sum(global::System.Collections.Generic.List value } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. ; global::SharedTypes.ListMarshaller.Free(__values_native); } @@ -3100,7 +3169,7 @@ public static partial int SumWithBuffer(global::System.Collections.Generic.List< } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. ; } @@ -3150,7 +3219,7 @@ public static unsafe partial int SumWithFreeTracking(global::System.Collections. } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __values_native__nativeSpan = global::SharedTypes.ListMarshaller.GetUnmanagedValuesDestination(__values_native, __values_native__numElements); for (int __i0 = 0; __i0 < __values_native__lastIndexMarshalled; ++__i0) @@ -3226,7 +3295,7 @@ public static partial int SumInArray(in global::System.Collections.Generic.List< } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. ; global::SharedTypes.ListMarshaller.Free(__values_native); } @@ -3272,7 +3341,7 @@ public static partial void Duplicate(ref global::System.Collections.Generic.List } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __values_native__numElements = numValues; global::SharedTypes.ListMarshaller.Free(__values_native); } @@ -3297,6 +3366,7 @@ public unsafe partial class Stateless [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial global::System.Collections.Generic.List CreateRange(int start, int end, out int numValues) { + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out numValues); global::System.Collections.Generic.List __retVal = default; byte* __retVal_native = default; @@ -3311,6 +3381,7 @@ public unsafe partial class Stateless __retVal_native = __PInvoke(start, end, __numValues_native); } + __invokeSucceeded = true; // Unmarshal - Convert native data to managed data. __retVal_native__numElements = numValues; __retVal = global::SharedTypes.ListMarshaller.AllocateContainerForManagedElements(__retVal_native, __retVal_native__numElements); @@ -3318,9 +3389,12 @@ public unsafe partial class Stateless } finally { - // Cleanup - Perform required cleanup. - __retVal_native__numElements = numValues; - global::SharedTypes.ListMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__numElements = numValues; + global::SharedTypes.ListMarshaller.Free(__retVal_native); + } } return __retVal; @@ -3344,6 +3418,7 @@ public unsafe partial class Stateless [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial void CreateRange_Out(int start, int end, out int numValues, out global::System.Collections.Generic.List res) { + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out numValues); System.Runtime.CompilerServices.Unsafe.SkipInit(out res); byte* __res_native = default; @@ -3358,6 +3433,7 @@ public static partial void CreateRange_Out(int start, int end, out int numValues __PInvoke(start, end, __numValues_native, &__res_native); } + __invokeSucceeded = true; // Unmarshal - Convert native data to managed data. __res_native__numElements = numValues; res = global::SharedTypes.ListMarshaller.AllocateContainerForManagedElements(__res_native, __res_native__numElements); @@ -3365,9 +3441,12 @@ public static partial void CreateRange_Out(int start, int end, out int numValues } finally { - // Cleanup - Perform required cleanup. - __res_native__numElements = numValues; - global::SharedTypes.ListMarshaller.Free(__res_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __res_native__numElements = numValues; + global::SharedTypes.ListMarshaller.Free(__res_native); + } } // Local P/Invoke @@ -3390,6 +3469,7 @@ public unsafe partial class Stateless [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial global::System.Collections.Generic.List GetLongBytes(long l) { + bool __invokeSucceeded = default; global::System.Collections.Generic.List __retVal = default; byte* __retVal_native = default; // Setup - Perform required setup. @@ -3401,6 +3481,7 @@ public unsafe partial class Stateless __retVal_native = __PInvoke(l); } + __invokeSucceeded = true; // Unmarshal - Convert native data to managed data. __retVal_native__numElements = 8; __retVal = global::SharedTypes.ListMarshaller.AllocateContainerForManagedElements(__retVal_native, __retVal_native__numElements); @@ -3408,9 +3489,12 @@ public unsafe partial class Stateless } finally { - // Cleanup - Perform required cleanup. - __retVal_native__numElements = 8; - global::SharedTypes.ListMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__numElements = 8; + global::SharedTypes.ListMarshaller.Free(__retVal_native); + } } return __retVal; @@ -3463,7 +3547,7 @@ public static partial bool AndAllMembers(global::System.Collections.Generic.List } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. _ = __pArray_native__lastIndexMarshalled; ; global::SharedTypes.ListMarshaller.Free(__pArray_native); @@ -3519,7 +3603,7 @@ public static partial bool AndAllMembersIn(in global::System.Collections.Generic } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. _ = __pArray_native__lastIndexMarshalled; ; global::SharedTypes.ListMarshaller.Free(__pArray_native); @@ -3582,7 +3666,7 @@ public static partial void NegateBools(ref global::System.Collections.Generic.Li } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. _ = __boolStruct_native__lastIndexMarshalled; __boolStruct_native__numElements = numValues; global::SharedTypes.ListMarshaller.Free(__boolStruct_native); @@ -3608,6 +3692,7 @@ public unsafe partial class Stateless [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial void NegateBools(global::System.Collections.Generic.List boolStruct, int numValues, out global::System.Collections.Generic.List pBoolStructOut) { + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out pBoolStructOut); byte* __boolStruct_native = default; byte* __pBoolStructOut_native = default; @@ -3634,6 +3719,7 @@ public static partial void NegateBools(global::System.Collections.Generic.List.AllocateContainerForManagedElements(__pBoolStructOut_native, __pBoolStructOut_native__numElements); @@ -3648,9 +3734,14 @@ public static partial void NegateBools(global::System.Collections.Generic.List.Free(__pBoolStructOut_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __pBoolStructOut_native__numElements = numValues; + global::SharedTypes.ListMarshaller.Free(__pBoolStructOut_native); + } + + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. _ = __boolStruct_native__lastIndexMarshalled; ; global::SharedTypes.ListMarshaller.Free(__boolStruct_native); @@ -3676,6 +3767,7 @@ public unsafe partial class Stateless [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial global::System.Collections.Generic.List NegateBools(global::System.Collections.Generic.List boolStruct, int numValues) { + bool __invokeSucceeded = default; byte* __boolStruct_native = default; global::System.Collections.Generic.List __retVal = default; byte* __retVal_native = default; @@ -3702,6 +3794,7 @@ public unsafe partial class Stateless __retVal_native = __PInvoke(__boolStruct_native, numValues); } + __invokeSucceeded = true; // Unmarshal - Convert native data to managed data. __retVal_native__numElements = numValues; __retVal = global::SharedTypes.ListMarshaller.AllocateContainerForManagedElements(__retVal_native, __retVal_native__numElements); @@ -3716,9 +3809,14 @@ public unsafe partial class Stateless } finally { - // Cleanup - Perform required cleanup. - __retVal_native__numElements = numValues; - global::SharedTypes.ListMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__numElements = numValues; + global::SharedTypes.ListMarshaller.Free(__retVal_native); + } + + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. _ = __boolStruct_native__lastIndexMarshalled; ; global::SharedTypes.ListMarshaller.Free(__boolStruct_native); @@ -3770,10 +3868,9 @@ public static partial int GuaranteedUnmarshal(out global::System.Collections.Gen // GuaranteedUnmarshal - Convert native data to managed data even in the case of an exception during the non-cleanup phases. __ret_native__numElements = 1; ret = global::LibraryImportGenerator.IntegrationTests.NativeExportsNE.Collections.Stateless.ListGuaranteedUnmarshal.AllocateContainerForManagedElementsFinally(__ret_native, __ret_native__numElements); + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __ret_native__numElements = 1; } - - // Cleanup - Perform required cleanup. - __ret_native__numElements = 1; } return __retVal; @@ -3822,10 +3919,9 @@ public static partial int GuaranteedUnmarshal(out global::System.Collections.Gen // GuaranteedUnmarshal - Convert native data to managed data even in the case of an exception during the non-cleanup phases. __ret_native__numElements = 1; ret = global::LibraryImportGenerator.IntegrationTests.NativeExportsNE.Collections.Stateless.ListGuaranteedUnmarshal.AllocateContainerForManagedElementsFinally(__ret_native, __ret_native__numElements); + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __ret_native__numElements = 1; } - - // Cleanup - Perform required cleanup. - __ret_native__numElements = 1; } return __retVal; @@ -3870,7 +3966,7 @@ public static partial int Sum(global::System.Collections.Generic.List value } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __values_native__marshaller.Free(); } @@ -3925,7 +4021,7 @@ public static unsafe partial int SumWithFreeTracking(global::System.Collections. } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. { System.ReadOnlySpan __values_native__nativeSpan = __values_native__marshaller.GetUnmanagedValuesDestination(); for (int __i0 = 0; __i0 < __values_native__lastIndexMarshalled; ++__i0) @@ -3979,7 +4075,7 @@ public static partial int SumInArray(in global::System.Collections.Generic.List< } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __values_native__marshaller.Free(); } @@ -4029,7 +4125,7 @@ public static partial void Duplicate(ref global::System.Collections.Generic.List } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __values_native__marshaller.Free(); } @@ -4053,6 +4149,7 @@ public unsafe partial class Stateful [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial global::System.Collections.Generic.List CreateRange(int start, int end, out int numValues) { + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out numValues); global::System.Collections.Generic.List __retVal = default; byte* __retVal_native = default; @@ -4068,6 +4165,7 @@ public unsafe partial class Stateful __retVal_native = __PInvoke(start, end, __numValues_native); } + __invokeSucceeded = true; // UnmarshalCapture - Capture the native data into marshaller instances in case conversion to managed data throws an exception. __retVal_native__marshaller.FromUnmanaged(__retVal_native); // Unmarshal - Convert native data to managed data. @@ -4077,8 +4175,11 @@ public unsafe partial class Stateful } finally { - // Cleanup - Perform required cleanup. - __retVal_native__marshaller.Free(); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__marshaller.Free(); + } } return __retVal; @@ -4102,6 +4203,7 @@ public unsafe partial class Stateful [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial void CreateRange_Out(int start, int end, out int numValues, out global::System.Collections.Generic.List res) { + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out numValues); System.Runtime.CompilerServices.Unsafe.SkipInit(out res); byte* __res_native = default; @@ -4117,6 +4219,7 @@ public static partial void CreateRange_Out(int start, int end, out int numValues __PInvoke(start, end, __numValues_native, &__res_native); } + __invokeSucceeded = true; // UnmarshalCapture - Capture the native data into marshaller instances in case conversion to managed data throws an exception. __res_native__marshaller.FromUnmanaged(__res_native); // Unmarshal - Convert native data to managed data. @@ -4126,8 +4229,11 @@ public static partial void CreateRange_Out(int start, int end, out int numValues } finally { - // Cleanup - Perform required cleanup. - __res_native__marshaller.Free(); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __res_native__marshaller.Free(); + } } // Local P/Invoke @@ -4150,6 +4256,7 @@ public unsafe partial class Stateful [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial global::System.Collections.Generic.List GetLongBytes(long l) { + bool __invokeSucceeded = default; global::System.Collections.Generic.List __retVal = default; byte* __retVal_native = default; // Setup - Perform required setup. @@ -4162,6 +4269,7 @@ public unsafe partial class Stateful __retVal_native = __PInvoke(l); } + __invokeSucceeded = true; // UnmarshalCapture - Capture the native data into marshaller instances in case conversion to managed data throws an exception. __retVal_native__marshaller.FromUnmanaged(__retVal_native); // Unmarshal - Convert native data to managed data. @@ -4171,8 +4279,11 @@ public unsafe partial class Stateful } finally { - // Cleanup - Perform required cleanup. - __retVal_native__marshaller.Free(); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__marshaller.Free(); + } } return __retVal; @@ -4230,7 +4341,7 @@ public static partial bool AndAllMembers(global::System.Collections.Generic.List } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. _ = __pArray_native__lastIndexMarshalled; __pArray_native__marshaller.Free(); } @@ -4290,7 +4401,7 @@ public static partial bool AndAllMembersIn(in global::System.Collections.Generic } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. _ = __pArray_native__lastIndexMarshalled; __pArray_native__marshaller.Free(); } @@ -4358,7 +4469,7 @@ public static partial void NegateBools(ref global::System.Collections.Generic.Li } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. _ = __boolStruct_native__lastIndexMarshalled; __boolStruct_native__marshaller.Free(); } @@ -4383,6 +4494,7 @@ public unsafe partial class Stateful [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial void NegateBools(global::System.Collections.Generic.List boolStruct, int numValues, out global::System.Collections.Generic.List pBoolStructOut) { + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out pBoolStructOut); byte* __boolStruct_native = default; byte* __pBoolStructOut_native = default; @@ -4415,6 +4527,7 @@ public static partial void NegateBools(global::System.Collections.Generic.List NegateBools(global::System.Collections.Generic.List boolStruct, int numValues) { + bool __invokeSucceeded = default; byte* __boolStruct_native = default; global::System.Collections.Generic.List __retVal = default; byte* __retVal_native = default; @@ -4490,6 +4609,7 @@ public unsafe partial class Stateful __retVal_native = __PInvoke(__boolStruct_native, numValues); } + __invokeSucceeded = true; // UnmarshalCapture - Capture the native data into marshaller instances in case conversion to managed data throws an exception. __retVal_native__marshaller.FromUnmanaged(__retVal_native); // Unmarshal - Convert native data to managed data. @@ -4507,8 +4627,13 @@ public unsafe partial class Stateful } finally { - // Cleanup - Perform required cleanup. - __retVal_native__marshaller.Free(); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__marshaller.Free(); + } + + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. _ = __boolStruct_native__lastIndexMarshalled; __boolStruct_native__marshaller.Free(); } @@ -4561,10 +4686,9 @@ public static partial int GuaranteedUnmarshal(out global::System.Collections.Gen { // GuaranteedUnmarshal - Convert native data to managed data even in the case of an exception during the non-cleanup phases. ret = __ret_native__marshaller.ToManagedFinally(); + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __ret_native__marshaller.Free(); } - - // Cleanup - Perform required cleanup. - __ret_native__marshaller.Free(); } return __retVal; @@ -4615,10 +4739,9 @@ public static partial int GuaranteedUnmarshal(out global::System.Collections.Gen { // GuaranteedUnmarshal - Convert native data to managed data even in the case of an exception during the non-cleanup phases. ret = __ret_native__marshaller.ToManagedFinally(); + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __ret_native__marshaller.Free(); } - - // Cleanup - Perform required cleanup. - __ret_native__marshaller.Free(); } return __retVal; @@ -4640,6 +4763,7 @@ internal unsafe partial class Stateless [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial void DeepDuplicateStrings(global::SharedTypes.StringContainer strings, out global::SharedTypes.StringContainer pStringsOut) { + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out pStringsOut); global::SharedTypes.StringContainerMarshaller.StringContainerNative __strings_native = default; global::SharedTypes.StringContainerMarshaller.StringContainerNative __pStringsOut_native = default; @@ -4651,13 +4775,19 @@ public static partial void DeepDuplicateStrings(global::SharedTypes.StringContai __PInvoke(__strings_native, &__pStringsOut_native); } + __invokeSucceeded = true; // Unmarshal - Convert native data to managed data. pStringsOut = global::SharedTypes.StringContainerMarshaller.Out.ConvertToManaged(__pStringsOut_native); } finally { - // Cleanup - Perform required cleanup. - global::SharedTypes.StringContainerMarshaller.Out.Free(__pStringsOut_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::SharedTypes.StringContainerMarshaller.Out.Free(__pStringsOut_native); + } + + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. global::SharedTypes.StringContainerMarshaller.In.Free(__strings_native); } @@ -4692,7 +4822,7 @@ public static partial void ReverseStrings(ref global::SharedTypes.StringContaine } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. global::SharedTypes.StringContainerMarshaller.Ref.Free(__strings_native); } @@ -4823,6 +4953,7 @@ internal unsafe partial class Stateless [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial global::SharedTypes.IntWrapper DoubleIntRef(global::SharedTypes.IntWrapper pInt) { + bool __invokeSucceeded = default; int* __pInt_native = default; global::SharedTypes.IntWrapper __retVal = default; int* __retVal_native = default; @@ -4834,13 +4965,19 @@ internal unsafe partial class Stateless __retVal_native = __PInvoke(__pInt_native); } + __invokeSucceeded = true; // Unmarshal - Convert native data to managed data. __retVal = global::SharedTypes.IntWrapperMarshaller.ConvertToManaged(__retVal_native); } finally { - // Cleanup - Perform required cleanup. - global::SharedTypes.IntWrapperMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::SharedTypes.IntWrapperMarshaller.Free(__retVal_native); + } + + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. global::SharedTypes.IntWrapperMarshaller.Free(__pInt_native); } @@ -4862,6 +4999,7 @@ internal unsafe partial class Stateless [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial global::SharedTypes.IntWrapperWithoutGetPinnableReference DoubleIntRef(global::SharedTypes.IntWrapperWithoutGetPinnableReference pInt) { + bool __invokeSucceeded = default; global::SharedTypes.IntWrapperWithoutGetPinnableReference __retVal = default; int* __retVal_native = default; try @@ -4872,13 +5010,17 @@ internal unsafe partial class Stateless __retVal_native = __PInvoke((int*)__pInt_native); } + __invokeSucceeded = true; // Unmarshal - Convert native data to managed data. __retVal = global::SharedTypes.IntWrapperWithoutGetPinnableReferenceMarshaller.ConvertToManaged(__retVal_native); } finally { - // Cleanup - Perform required cleanup. - global::SharedTypes.IntWrapperWithoutGetPinnableReferenceMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::SharedTypes.IntWrapperWithoutGetPinnableReferenceMarshaller.Free(__retVal_native); + } } return __retVal; @@ -5094,6 +5236,7 @@ internal unsafe partial class Stateful [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial global::SharedTypes.IntWrapperWithoutGetPinnableReference DoubleIntRef(global::SharedTypes.IntWrapperWithoutGetPinnableReference pInt) { + bool __invokeSucceeded = default; global::SharedTypes.IntWrapperWithoutGetPinnableReference __retVal = default; int* __retVal_native = default; try @@ -5104,13 +5247,17 @@ internal unsafe partial class Stateful __retVal_native = __PInvoke((int*)__pInt_native); } + __invokeSucceeded = true; // Unmarshal - Convert native data to managed data. __retVal = global::SharedTypes.IntWrapperWithoutGetPinnableReferenceMarshaller.ConvertToManaged(__retVal_native); } finally { - // Cleanup - Perform required cleanup. - global::SharedTypes.IntWrapperWithoutGetPinnableReferenceMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::SharedTypes.IntWrapperWithoutGetPinnableReferenceMarshaller.Free(__retVal_native); + } } return __retVal; @@ -5131,6 +5278,7 @@ internal unsafe partial class Stateful [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial global::SharedTypes.IntWrapperWithoutGetPinnableReference DoubleIntRefNoAlloc(global::SharedTypes.IntWrapperWithoutGetPinnableReference pInt) { + bool __invokeSucceeded = default; int* __pInt_native = default; global::SharedTypes.IntWrapperWithoutGetPinnableReference __retVal = default; int* __retVal_native = default; @@ -5148,13 +5296,17 @@ internal unsafe partial class Stateful __retVal_native = __PInvoke(__pInt_native); } + __invokeSucceeded = true; // Unmarshal - Convert native data to managed data. __retVal = global::SharedTypes.IntWrapperWithoutGetPinnableReferenceMarshaller.ConvertToManaged(__retVal_native); } finally { - // Cleanup - Perform required cleanup. - global::SharedTypes.IntWrapperWithoutGetPinnableReferenceMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::SharedTypes.IntWrapperWithoutGetPinnableReferenceMarshaller.Free(__retVal_native); + } } return __retVal; @@ -5175,6 +5327,7 @@ internal unsafe partial class Stateful [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial global::SharedTypes.IntWrapper DoubleIntRef(global::SharedTypes.IntWrapper pInt) { + bool __invokeSucceeded = default; int* __pInt_native = default; global::SharedTypes.IntWrapper __retVal = default; int* __retVal_native = default; @@ -5191,6 +5344,7 @@ internal unsafe partial class Stateful __retVal_native = __PInvoke(__pInt_native); } + __invokeSucceeded = true; // UnmarshalCapture - Capture the native data into marshaller instances in case conversion to managed data throws an exception. __retVal_native__marshaller.FromUnmanaged(__retVal_native); // Unmarshal - Convert native data to managed data. @@ -5198,8 +5352,13 @@ internal unsafe partial class Stateful } finally { - // Cleanup - Perform required cleanup. - __retVal_native__marshaller.Free(); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__marshaller.Free(); + } + + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __pInt_native__marshaller.Free(); } @@ -5246,10 +5405,9 @@ public static partial int GuaranteedUnmarshal(out int ret) { // GuaranteedUnmarshal - Convert native data to managed data even in the case of an exception during the non-cleanup phases. __retVal = __retVal_native__marshaller.ToManagedFinally(); + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__marshaller.Free(); } - - // Cleanup - Perform required cleanup. - __retVal_native__marshaller.Free(); } return __retVal; @@ -5282,7 +5440,7 @@ public static partial void ReverseReplaceString(ref string s) } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__s_native); } @@ -5624,6 +5782,7 @@ public unsafe partial class NativeExportsSafeHandle [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] private static partial global::LibraryImportGenerator.IntegrationTests.NativeExportsNE.NativeExportsSafeHandle AllocateHandle() { + bool __invokeSucceeded = default; global::LibraryImportGenerator.IntegrationTests.NativeExportsNE.NativeExportsSafeHandle __retVal = default; nint __retVal_native = default; // Setup - Perform required setup. @@ -5634,6 +5793,7 @@ public unsafe partial class NativeExportsSafeHandle __retVal_native = __PInvoke(); } + __invokeSucceeded = true; // UnmarshalCapture - Capture the native data into marshaller instances in case conversion to managed data throws an exception. __retVal_native__marshaller.FromUnmanaged(__retVal_native); // Unmarshal - Convert native data to managed data. @@ -5641,8 +5801,11 @@ public unsafe partial class NativeExportsSafeHandle } finally { - // Cleanup - Perform required cleanup. - __retVal_native__marshaller.Free(); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__marshaller.Free(); + } } return __retVal; @@ -5661,6 +5824,7 @@ unsafe partial class NativeExportsNE [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial global::LibraryImportGenerator.IntegrationTests.NativeExportsNE.NativeExportsSafeHandle AllocateHandle() { + bool __invokeSucceeded = default; global::LibraryImportGenerator.IntegrationTests.NativeExportsNE.NativeExportsSafeHandle __retVal = default; nint __retVal_native = default; // Setup - Perform required setup. @@ -5671,6 +5835,7 @@ unsafe partial class NativeExportsNE __retVal_native = __PInvoke(); } + __invokeSucceeded = true; // UnmarshalCapture - Capture the native data into marshaller instances in case conversion to managed data throws an exception. __retVal_native__marshaller.FromUnmanaged(__retVal_native); // Unmarshal - Convert native data to managed data. @@ -5678,8 +5843,11 @@ unsafe partial class NativeExportsNE } finally { - // Cleanup - Perform required cleanup. - __retVal_native__marshaller.Free(); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__marshaller.Free(); + } } return __retVal; @@ -5697,6 +5865,7 @@ unsafe partial class NativeExportsNE [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial void AllocateHandle(out global::LibraryImportGenerator.IntegrationTests.NativeExportsNE.NativeExportsSafeHandle handle) { + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out handle); nint __handle_native = default; // Setup - Perform required setup. @@ -5707,6 +5876,7 @@ public static partial void AllocateHandle(out global::LibraryImportGenerator.Int __PInvoke(&__handle_native); } + __invokeSucceeded = true; // UnmarshalCapture - Capture the native data into marshaller instances in case conversion to managed data throws an exception. __handle_native__marshaller.FromUnmanaged(__handle_native); // Unmarshal - Convert native data to managed data. @@ -5714,8 +5884,11 @@ public static partial void AllocateHandle(out global::LibraryImportGenerator.Int } finally { - // Cleanup - Perform required cleanup. - __handle_native__marshaller.Free(); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __handle_native__marshaller.Free(); + } } // Local P/Invoke @@ -5775,7 +5948,7 @@ public static partial bool IsHandleAlive(global::LibraryImportGenerator.Integrat } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __handle_native__marshaller.Free(); } @@ -5824,7 +5997,7 @@ public static partial void ModifyHandle(ref global::LibraryImportGenerator.Integ handle = __handle_native__marshaller.ToManagedFinally(); } - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __handle_native__marshaller.Free(); } @@ -5902,6 +6075,7 @@ public unsafe partial class SetLastError public static partial string SetError_NonBlittableSignature(int error, bool shouldSetError, string errorString) { int __lastError; + bool __invokeSucceeded = default; byte __shouldSetError_native = default; string __retVal = default; ushort* __retVal_native = default; @@ -5917,13 +6091,17 @@ public static partial string SetError_NonBlittableSignature(int error, bool shou __lastError = System.Runtime.InteropServices.Marshal.GetLastSystemError(); } + __invokeSucceeded = true; // Unmarshal - Convert native data to managed data. __retVal = global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.ConvertToManaged(__retVal_native); } finally { - // Cleanup - Perform required cleanup. - global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__retVal_native); + } } System.Runtime.InteropServices.Marshal.SetLastPInvokeError(__lastError); @@ -6016,7 +6194,7 @@ public static partial int SumInArray(in global::System.Span values, int num } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __values_native__marshaller.Free(); } @@ -6058,7 +6236,7 @@ public static partial void Duplicate(ref global::System.Span values, int nu } finally { - // Cleanup - Perform required cleanup. + // CleanupCallerAllocated - Perform cleanup of caller allocated resources. __values_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.SpanMarshaller.Free(__values_native); } @@ -6080,6 +6258,7 @@ public unsafe partial class Span [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial global::System.Span CreateRange(int start, int end, out int numValues) { + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out numValues); global::System.Span __retVal = default; int* __retVal_native = default; @@ -6094,6 +6273,7 @@ public unsafe partial class Span __retVal_native = __PInvoke(start, end, __numValues_native); } + __invokeSucceeded = true; // Unmarshal - Convert native data to managed data. __retVal_native__numElements = numValues; __retVal = global::System.Runtime.InteropServices.Marshalling.SpanMarshaller.AllocateContainerForManagedElements(__retVal_native, __retVal_native__numElements); @@ -6101,9 +6281,12 @@ public unsafe partial class Span } finally { - // Cleanup - Perform required cleanup. - __retVal_native__numElements = numValues; - global::System.Runtime.InteropServices.Marshalling.SpanMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__numElements = numValues; + global::System.Runtime.InteropServices.Marshalling.SpanMarshaller.Free(__retVal_native); + } } return __retVal; @@ -6124,6 +6307,7 @@ public unsafe partial class Span [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial void CreateRange_Out(int start, int end, out int numValues, out global::System.Span res) { + bool __invokeSucceeded = default; System.Runtime.CompilerServices.Unsafe.SkipInit(out numValues); res = default; int* __res_native = default; @@ -6138,6 +6322,7 @@ public static partial void CreateRange_Out(int start, int end, out int numValues __PInvoke(start, end, __numValues_native, &__res_native); } + __invokeSucceeded = true; // Unmarshal - Convert native data to managed data. __res_native__numElements = numValues; res = global::System.Runtime.InteropServices.Marshalling.SpanMarshaller.AllocateContainerForManagedElements(__res_native, __res_native__numElements); @@ -6145,9 +6330,12 @@ public static partial void CreateRange_Out(int start, int end, out int numValues } finally { - // Cleanup - Perform required cleanup. - __res_native__numElements = numValues; - global::System.Runtime.InteropServices.Marshalling.SpanMarshaller.Free(__res_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __res_native__numElements = numValues; + global::System.Runtime.InteropServices.Marshalling.SpanMarshaller.Free(__res_native); + } } // Local P/Invoke @@ -6167,6 +6355,7 @@ public unsafe partial class Span [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial global::System.Span GetLongBytes(long l) { + bool __invokeSucceeded = default; global::System.Span __retVal = default; byte* __retVal_native = default; // Setup - Perform required setup. @@ -6178,6 +6367,7 @@ public unsafe partial class Span __retVal_native = __PInvoke(l); } + __invokeSucceeded = true; // Unmarshal - Convert native data to managed data. __retVal_native__numElements = 8; __retVal = global::System.Runtime.InteropServices.Marshalling.SpanMarshaller.AllocateContainerForManagedElements(__retVal_native, __retVal_native__numElements); @@ -6185,9 +6375,12 @@ public unsafe partial class Span } finally { - // Cleanup - Perform required cleanup. - __retVal_native__numElements = 8; - global::System.Runtime.InteropServices.Marshalling.SpanMarshaller.Free(__retVal_native); + if (__invokeSucceeded) + { + // CleanupCalleeAllocated - Perform cleanup of callee allocated resources. + __retVal_native__numElements = 8; + global::System.Runtime.InteropServices.Marshalling.SpanMarshaller.Free(__retVal_native); + } } return __retVal; @@ -6242,7 +6435,7 @@ public static partial bool AndAllMembers(global::System.Span