From c4a9e3a940b824a7baa9bd3e3a04ff34ff40fa89 Mon Sep 17 00:00:00 2001 From: Alexander Corrado Date: Sun, 14 Aug 2011 17:45:50 -0400 Subject: [PATCH] Fix invalid IL and memory leak returning by value (Itanium) and/or as value type --- src/Mono.Cxxi/Abi/CppAbi.cs | 4 +++- src/Mono.Cxxi/Abi/Impl/ItaniumAbi.cs | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Mono.Cxxi/Abi/CppAbi.cs b/src/Mono.Cxxi/Abi/CppAbi.cs index 35381d9c..8456ab92 100644 --- a/src/Mono.Cxxi/Abi/CppAbi.cs +++ b/src/Mono.Cxxi/Abi/CppAbi.cs @@ -60,6 +60,7 @@ namespace Mono.Cxxi.Abi { protected static readonly MethodInfo cppip_managedalloc = typeof (CppInstancePtr).GetProperty ("IsManagedAlloc").GetGetMethod (); protected static readonly MethodInfo cppip_tomanaged = typeof (CppInstancePtr).GetMethod ("ToManaged", BindingFlags.Static | BindingFlags.NonPublic, null, new Type [] { typeof (IntPtr) }, null); protected static readonly MethodInfo cppip_tomanaged_size = typeof (CppInstancePtr).GetMethod ("ToManaged", BindingFlags.Static | BindingFlags.NonPublic, null, new Type [] { typeof (IntPtr), typeof (int) }, null); + protected static readonly MethodInfo cppip_dispose = typeof (CppInstancePtr).GetMethod ("Dispose"); protected static readonly ConstructorInfo cppip_fromnative = typeof (CppInstancePtr).GetConstructor (new Type [] { typeof (IntPtr) }); protected static readonly ConstructorInfo cppip_fromsize = typeof (CppInstancePtr).GetConstructor (BindingFlags.Instance | BindingFlags.NonPublic, null, new Type [] { typeof (int) }, null); protected static readonly ConstructorInfo cppip_fromtype_managed = typeof (CppInstancePtr).GetConstructor (BindingFlags.Instance | BindingFlags.NonPublic, null, @@ -776,8 +777,9 @@ namespace Mono.Cxxi.Abi { } else if (targetType.IsValueType) { il.Emit (OpCodes.Ldtoken, targetType); + il.Emit (OpCodes.Call, type_gettypefromhandle); il.Emit (OpCodes.Call, marshal_ptrtostructure); - il.Emit (OpCodes.Unbox, targetType); + il.Emit (OpCodes.Unbox_Any, targetType); } } diff --git a/src/Mono.Cxxi/Abi/Impl/ItaniumAbi.cs b/src/Mono.Cxxi/Abi/Impl/ItaniumAbi.cs index 3a4f7b98..bf978062 100644 --- a/src/Mono.Cxxi/Abi/Impl/ItaniumAbi.cs +++ b/src/Mono.Cxxi/Abi/Impl/ItaniumAbi.cs @@ -250,6 +250,10 @@ namespace Mono.Cxxi.Abi { if (hiddenReturnByValue) { EmitCreateCppObjectFromNative (il, method.ReturnType, returnValue); + + // FIXME: This dispose should prolly be in a Finally block.. + il.Emit (OpCodes.Ldloca, returnValue); + il.Emit (OpCodes.Call, cppip_dispose); } }