Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leaks of classes when using the generic marshaler to dispose #800

Merged
merged 3 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/WinRT.Runtime/FundamentalMarshalers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions src/WinRT.Runtime/Marshalers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ static MarshalGeneric()
FromManaged = BindFromManaged();
CopyManaged = BindCopyManaged();
DisposeMarshaler = BindDisposeMarshaler();
DisposeAbi = BindDisposeAbi();
CreateMarshalerArray = BindCreateMarshalerArray();
GetAbiArray = BindGetAbiArray();
FromAbiArray = BindFromAbiArray();
Expand Down Expand Up @@ -434,6 +435,16 @@ private static Action<object> BindDisposeMarshaler()
new[] { Expression.Convert(parms[0], MarshalerType) }), parms).Compile();
}

internal static readonly Action<object> DisposeAbi;
private static Action<object> 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<Action<object>>(
Expression.Call(disposeAbi, new[] { Expression.Convert(parms[0], AbiType) }), parms).Compile();
}

internal static readonly Func<T[], object> CreateMarshalerArray;
private static Func<T[], object> BindCreateMarshalerArray()
{
Expand Down Expand Up @@ -1154,7 +1165,7 @@ static Marshaler()
FromManaged = MarshalGeneric<T>.FromManaged;
CopyManaged = MarshalGeneric<T>.CopyManaged;
DisposeMarshaler = MarshalGeneric<T>.DisposeMarshaler;
DisposeAbi = (object box) => { };
DisposeAbi = MarshalGeneric<T>.DisposeAbi;
CreateMarshalerArray = (T[] array) => MarshalGeneric<T>.CreateMarshalerArray(array);
GetAbiArray = (object box) => MarshalGeneric<T>.GetAbiArray(box);
FromAbiArray = (object box) => MarshalGeneric<T>.FromAbiArray(box);
Expand Down Expand Up @@ -1255,7 +1266,7 @@ static Marshaler()
FromManaged = MarshalGeneric<T>.FromManaged;
CopyManaged = MarshalGeneric<T>.CopyManaged;
DisposeMarshaler = MarshalGeneric<T>.DisposeMarshaler;
DisposeAbi = (object box) => { };
DisposeAbi = MarshalGeneric<T>.DisposeAbi;
CreateMarshalerArray = (T[] array) => MarshalGeneric<T>.CreateMarshalerArray(array);
GetAbiArray = (object box) => MarshalGeneric<T>.GetAbiArray(box);
FromAbiArray = (object box) => MarshalGeneric<T>.FromAbiArray(box);
Expand Down