summaryrefslogtreecommitdiff
path: root/src/bindings
diff options
context:
space:
mode:
authorLucas Cavalcante de Sousa <lucas@expertisesolutions.com.br>2020-02-28 15:58:08 +0000
committerFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2020-03-23 14:18:16 -0300
commit117450e3fac0be15c9cb292cb158d9398b6f30ef (patch)
treef22fc424f0e4e09151744057991c6a683f94dbce /src/bindings
parente5cec5dacca53017d1fb260181993f2a81264577 (diff)
C#: Update C# code-generation to use a new ICustomMarshaler in some string usages that were leaking
When `C` calls a function that return/has an out string and it was overwritten by `C#` inherit class the `C` portion wasn't cleaning its copy. Now, when a `C` calls a `C#` delegate function, `Strings` that are `out` values or `return` values use a new marshaler (specific to this case) that uses Eina short lived strings (`Eina_Slstr`) instead of duplicating it with `strdup`, so at some point, the string passed to `C` is deleted. To do so, a `direction_context` (a new `Context` at `generation_contexts.hh`) was created. It is only used when a C# delegate is being called from C (so this context is only set in `function_definition.hh` and `property_definition.hh`, where it is set to `native_to_manage` to indicate that it is a native call to a managed function). When this `direction_context` is set and the `String` being marshaled is not marked with an `@move` tag and it is an `out` or `return` value, the new `StringOutMarshaler` (implemented at `iwrapper.cs`) is used (instead of `StringKeepOwnershipMarshaler`). When marshaling a managed data to native this marshaler uses eina short lived string (`Eina_Slstr`) that will be automatically deleted. This delete is bounded to "the loop of the current thread or until the clear function is called explicitly" as said at `src/lib/eina/eina_slstr.h`. Reviewed-by: Felipe Magno de Almeida <felipe@expertisesolutions.com.br> Differential Revision: https://phab.enlightenment.org/D11434
Diffstat (limited to 'src/bindings')
-rw-r--r--src/bindings/mono/eina_mono/eina_common.cs8
-rw-r--r--src/bindings/mono/eo_mono/iwrapper.cs39
2 files changed, 47 insertions, 0 deletions
diff --git a/src/bindings/mono/eina_mono/eina_common.cs b/src/bindings/mono/eina_mono/eina_common.cs
index a4f2ff4c5f..014e77d020 100644
--- a/src/bindings/mono/eina_mono/eina_common.cs
+++ b/src/bindings/mono/eina_mono/eina_common.cs
@@ -55,6 +55,9 @@ internal static partial class NativeCustomExportFunctions
55 efl_mono_native_free_addr_get(); 55 efl_mono_native_free_addr_get();
56 [DllImport(efl.Libs.CustomExports)] public static extern IntPtr 56 [DllImport(efl.Libs.CustomExports)] public static extern IntPtr
57 efl_mono_native_efl_unref_addr_get(); 57 efl_mono_native_efl_unref_addr_get();
58
59 [DllImport(efl.Libs.Eina)] public static extern IntPtr
60 eina_slstr_copy_new(string str);
58} 61}
59 62
60/// <summary>Wrapper around native memory DllImport'd functions. 63/// <summary>Wrapper around native memory DllImport'd functions.
@@ -94,6 +97,11 @@ public static class MemoryNative
94 return NativeCustomExportFunctions.efl_mono_native_strdup(str); 97 return NativeCustomExportFunctions.efl_mono_native_strdup(str);
95 } 98 }
96 99
100 public static IntPtr SlstrCopyNew(string str)
101 {
102 return NativeCustomExportFunctions.eina_slstr_copy_new(str);
103 }
104
97 /// <summary> 105 /// <summary>
98 /// Retrieves an instance of a string for use in program. 106 /// Retrieves an instance of a string for use in program.
99 /// <para>Since EFL 1.23.</para> 107 /// <para>Since EFL 1.23.</para>
diff --git a/src/bindings/mono/eo_mono/iwrapper.cs b/src/bindings/mono/eo_mono/iwrapper.cs
index ceae250bc9..626777f7d5 100644
--- a/src/bindings/mono/eo_mono/iwrapper.cs
+++ b/src/bindings/mono/eo_mono/iwrapper.cs
@@ -1508,6 +1508,45 @@ class StringPassOwnershipMarshaler : ICustomMarshaler
1508 static private StringPassOwnershipMarshaler marshaler; 1508 static private StringPassOwnershipMarshaler marshaler;
1509} 1509}
1510 1510
1511
1512class StringOutMarshaler: ICustomMarshaler
1513{
1514 public object MarshalNativeToManaged(IntPtr pNativeData)
1515 {
1516 return Eina.StringConversion.NativeUtf8ToManagedString(pNativeData);
1517 }
1518
1519 public IntPtr MarshalManagedToNative(object managedObj)
1520 {
1521 return Eina.MemoryNative.SlstrCopyNew((string)managedObj);
1522 }
1523
1524 public void CleanUpNativeData(IntPtr pNativeData)
1525 {
1526 }
1527
1528 public void CleanUpManagedData(object managedObj)
1529 {
1530 }
1531
1532 public int GetNativeDataSize()
1533 {
1534 return -1;
1535 }
1536
1537 internal static ICustomMarshaler GetInstance(string cookie)
1538 {
1539 if (marshaler == null)
1540 {
1541 marshaler = new StringOutMarshaler();
1542 }
1543
1544 return marshaler;
1545 }
1546
1547 static private StringOutMarshaler marshaler;
1548}
1549
1511class StringKeepOwnershipMarshaler: ICustomMarshaler 1550class StringKeepOwnershipMarshaler: ICustomMarshaler
1512{ 1551{
1513 public object MarshalNativeToManaged(IntPtr pNativeData) 1552 public object MarshalNativeToManaged(IntPtr pNativeData)