From 86ed1178db9225254a5cff811e530e8c67723231 Mon Sep 17 00:00:00 2001 From: Manodasan Wignarajah Date: Sun, 30 Jan 2022 02:25:04 -0800 Subject: [PATCH] Fix issue where dispose ABI wasn't being called. --- src/WinRT.Runtime/Marshalers.cs | 2 +- src/WinRT.Runtime/Projections/Type.cs | 2 +- src/cswinrt/code_writers.h | 29 ++++++++++++++++++--------- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/WinRT.Runtime/Marshalers.cs b/src/WinRT.Runtime/Marshalers.cs index a36dfa195..b924a338d 100644 --- a/src/WinRT.Runtime/Marshalers.cs +++ b/src/WinRT.Runtime/Marshalers.cs @@ -1387,7 +1387,7 @@ unsafe void CopyEnum(object value, IntPtr dest) FromManaged = MarshalNonBlittable.FromManaged; CopyManaged = MarshalNonBlittable.CopyManaged; DisposeMarshaler = MarshalNonBlittable.DisposeMarshaler; - DisposeAbi = (object box) => { }; + DisposeAbi = MarshalNonBlittable.DisposeAbi; CreateMarshalerArray = (T[] array) => MarshalNonBlittable.CreateMarshalerArray(array); GetAbiArray = (object box) => MarshalNonBlittable.GetAbiArray(box); FromAbiArray = (object box) => MarshalNonBlittable.FromAbiArray(box); diff --git a/src/WinRT.Runtime/Projections/Type.cs b/src/WinRT.Runtime/Projections/Type.cs index ed940c0ba..fc5ccc683 100644 --- a/src/WinRT.Runtime/Projections/Type.cs +++ b/src/WinRT.Runtime/Projections/Type.cs @@ -110,7 +110,7 @@ public static unsafe void CopyManaged(global::System.Type arg, IntPtr dest) => *(Type*)dest.ToPointer() = FromManaged(arg); public static void DisposeMarshaler(Marshaler m) { m.Dispose(); } - public static void DisposeAbi(Type abi) { } + public static void DisposeAbi(Type abi) { MarshalString.DisposeAbi(abi.Name); } public static string GetGuidSignature() { diff --git a/src/cswinrt/code_writers.h b/src/cswinrt/code_writers.h index 715b494ca..7f804684a 100644 --- a/src/cswinrt/code_writers.h +++ b/src/cswinrt/code_writers.h @@ -7026,7 +7026,7 @@ var m = new Marshaler();)", if (have_disposers) { w.write(R"( -Func dispose = () => { m.Dispose(); return false; }; +bool success = false; try {)"); } @@ -7041,6 +7041,7 @@ try m.__abi = new %() { %}; +% return m;)", abi_type, [&](writer& w) @@ -7074,15 +7075,18 @@ return m;)", m.marshaler_type, m.param_name); } - }); + }, + have_disposers ? "success = true;" : ""); if (have_disposers) { w.write(R"( } -catch (Exception) when (dispose()) +finally { -// Will never execute -return default; +if (!success) +{ +m.Dispose(); +} } )"); } @@ -7198,14 +7202,21 @@ public static void DisposeMarshaler(Marshaler m) % have_disposers ? "=> m.Dispose();" : "{}"); w.write(R"( -public static void DisposeAbi(% abi){ /*todo*/ } +public static void DisposeAbi(% abi) +{ +%} } - )", - abi_type); + abi_type, + bind_each([](writer& w, abi_marshaler const& m) + { + if (m.is_value_type) return; + w.write("%.DisposeAbi(abi.%);\n", + m.marshaler_type, + m.param_name); + }, marshalers)); } - void write_factory_class_inheritance(writer& w, TypeDef const& type) { auto delimiter{ ", " };