From 7c543d3c860c8c2d6979c55a0460d4ad3c983291 Mon Sep 17 00:00:00 2001 From: Lauro Moura Date: Thu, 15 Mar 2018 20:35:17 -0300 Subject: [PATCH] csharp: Change policy on ptr(struct) owned calls When transferring the ownership of a ptr(struct) from Unamanaged to managed, we should marshal the reference-typed fields or they can point to Managed memory that would make no sense to access from C (for example, strings would return garbage). The downside is that it can cause potential leaks if the receiver of the memory do not free it. In the current state of the EFL api this can happen in two places: - Efl.Ui.List.SegArray::remove return -> ptr(Efl.Ui.List.LayoutItem) @owned - Efl.Ui.Focus.Manager::fetch return -> ptr(Efl.Ui.Focus.Relations) @owned The resources copied by both structs may leak when those functions are overriden in C# and the values returned to the C code. Also hide some internal stuff instead of exporting it and generate implicit conversion operators for struct external/internal. --- src/bindings/mono/eina_mono/eina_common.cs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/bindings/mono/eina_mono/eina_common.cs b/src/bindings/mono/eina_mono/eina_common.cs index 4bd93ce25b..8bccc06ea4 100644 --- a/src/bindings/mono/eina_mono/eina_common.cs +++ b/src/bindings/mono/eina_mono/eina_common.cs @@ -110,11 +110,9 @@ public static class PrimitiveConversion public static IntPtr ManagedToPointerAlloc(T man) { - GCHandle pinnedData = GCHandle.Alloc(man, GCHandleType.Pinned); - IntPtr ptr = pinnedData.AddrOfPinnedObject(); - IntPtr nat = MemoryNative.AllocCopy(ptr, Marshal.SizeOf()); - pinnedData.Free(); - return nat; + IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf()); + Marshal.StructureToPtr(man, ptr, false); + return ptr; } }