From 838dd0ca65b02b085a814c24dcd8dc64574421c6 Mon Sep 17 00:00:00 2001 From: Manodasan Wignarajah Date: Tue, 13 Apr 2021 07:40:00 -0700 Subject: [PATCH 1/2] Fix memory leaks in classes when using generic marshaler to dispose. --- src/WinRT.Runtime/FundamentalMarshalers.cs | 2 +- src/WinRT.Runtime/Marshalers.cs | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/WinRT.Runtime/FundamentalMarshalers.cs b/src/WinRT.Runtime/FundamentalMarshalers.cs index 6ffd8823c..5ab5e0944 100644 --- a/src/WinRT.Runtime/FundamentalMarshalers.cs +++ b/src/WinRT.Runtime/FundamentalMarshalers.cs @@ -14,7 +14,7 @@ internal struct Boolean public static Boolean FromManaged(bool value) => GetAbi(value); public static unsafe void CopyManaged(bool arg, IntPtr dest) => *(byte*)dest.ToPointer() = FromManaged(arg).value; public static void DisposeMarshaler(bool m) { } - public static void DisposeAbi(byte abi) { } + public static void DisposeAbi(Boolean abi) { } } internal struct Char diff --git a/src/WinRT.Runtime/Marshalers.cs b/src/WinRT.Runtime/Marshalers.cs index d4ea4bcb0..45ffb9d32 100644 --- a/src/WinRT.Runtime/Marshalers.cs +++ b/src/WinRT.Runtime/Marshalers.cs @@ -359,6 +359,7 @@ static MarshalGeneric() FromManaged = BindFromManaged(); CopyManaged = BindCopyManaged(); DisposeMarshaler = BindDisposeMarshaler(); + DisposeAbi = BindDisposeAbi(); CreateMarshalerArray = BindCreateMarshalerArray(); GetAbiArray = BindGetAbiArray(); FromAbiArray = BindFromAbiArray(); @@ -434,6 +435,16 @@ private static Action BindDisposeMarshaler() new[] { Expression.Convert(parms[0], MarshalerType) }), parms).Compile(); } + internal static readonly Action DisposeAbi; + private static Action BindDisposeAbi() + { + var disposeAbi = HelperType.GetMethod("DisposeAbi", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); + if (disposeAbi == null) return null; + var parms = new[] { Expression.Parameter(typeof(object), "arg") }; + return Expression.Lambda>( + Expression.Call(disposeAbi, new[] { Expression.Convert(parms[0], AbiType) }), parms).Compile(); + } + internal static readonly Func CreateMarshalerArray; private static Func BindCreateMarshalerArray() { @@ -1255,7 +1266,7 @@ static Marshaler() FromManaged = MarshalGeneric.FromManaged; CopyManaged = MarshalGeneric.CopyManaged; DisposeMarshaler = MarshalGeneric.DisposeMarshaler; - DisposeAbi = (object box) => { }; + DisposeAbi = MarshalGeneric.DisposeAbi; CreateMarshalerArray = (T[] array) => MarshalGeneric.CreateMarshalerArray(array); GetAbiArray = (object box) => MarshalGeneric.GetAbiArray(box); FromAbiArray = (object box) => MarshalGeneric.FromAbiArray(box); From 282dcb5eac7047fce1c19f2eb92d9750091220f0 Mon Sep 17 00:00:00 2001 From: Manodasan Wignarajah Date: Tue, 13 Apr 2021 08:22:26 -0700 Subject: [PATCH 2/2] Add one more missing dispose. --- src/WinRT.Runtime/Marshalers.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WinRT.Runtime/Marshalers.cs b/src/WinRT.Runtime/Marshalers.cs index 45ffb9d32..8d422a7c2 100644 --- a/src/WinRT.Runtime/Marshalers.cs +++ b/src/WinRT.Runtime/Marshalers.cs @@ -1165,7 +1165,7 @@ static Marshaler() FromManaged = MarshalGeneric.FromManaged; CopyManaged = MarshalGeneric.CopyManaged; DisposeMarshaler = MarshalGeneric.DisposeMarshaler; - DisposeAbi = (object box) => { }; + DisposeAbi = MarshalGeneric.DisposeAbi; CreateMarshalerArray = (T[] array) => MarshalGeneric.CreateMarshalerArray(array); GetAbiArray = (object box) => MarshalGeneric.GetAbiArray(box); FromAbiArray = (object box) => MarshalGeneric.FromAbiArray(box);