From 3875c3d949694e5f5744912cfdc469b329f86747 Mon Sep 17 00:00:00 2001 From: Lauro Moura Date: Wed, 4 Dec 2019 10:24:00 -0300 Subject: csharp: WIP - Avoid string leaks These dicts will tie the lifetime of the native strings to the lifetime of the C# wrapper they are used with. PS: What about strings in struct fields? --- src/bindings/mono/eo_mono/EoWrapper.cs | 9 ++++++++- src/bindings/mono/eo_mono/iwrapper.cs | 5 +++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/bindings/mono/eo_mono/EoWrapper.cs b/src/bindings/mono/eo_mono/EoWrapper.cs index 554f4ec9bd..cc57be910c 100644 --- a/src/bindings/mono/eo_mono/EoWrapper.cs +++ b/src/bindings/mono/eo_mono/EoWrapper.cs @@ -19,6 +19,7 @@ using System.Runtime.CompilerServices; using System.Threading; using System.Reflection; using System.Collections; +using System.Collections.Concurrent; namespace Efl { @@ -44,6 +45,9 @@ public abstract class EoWrapper : IWrapper, IDisposable private static Efl.EventCb ownershipUniqueDelegate = new Efl.EventCb(OwnershipUniqueCallback); private static Efl.EventCb ownershipSharedDelegate = new Efl.EventCb(OwnershipSharedCallback); + private ConcurrentDictionary cached_strings = new ConcurrentDictionary(); + private ConcurrentDictionary cached_stringshares = new ConcurrentDictionary(); + private Hashtable keyValueHash = null; /// Constructor to be used when objects are expected to be constructed from native code. @@ -195,9 +199,12 @@ public abstract class EoWrapper : IWrapper, IDisposable { Efl.Eo.Globals.efl_mono_thread_safe_native_dispose(handle); } - Monitor.Exit(Efl.All.InitLock); } + + // Are these threadsafe? + Efl.Eo.Globals.free_dict_values(cached_strings); + Efl.Eo.Globals.free_stringshare_values(cached_stringshares); } /// Turns the native pointer into a string representation. diff --git a/src/bindings/mono/eo_mono/iwrapper.cs b/src/bindings/mono/eo_mono/iwrapper.cs index e5a357833f..d088b70a99 100644 --- a/src/bindings/mono/eo_mono/iwrapper.cs +++ b/src/bindings/mono/eo_mono/iwrapper.cs @@ -19,6 +19,7 @@ using System; using System.Runtime.InteropServices; using System.Runtime.CompilerServices; using System.Collections.Generic; +using System.Collections.Concurrent; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Reflection; @@ -528,7 +529,7 @@ public static class Globals Efl.Eo.Globals.efl_mono_wrapper_supervisor_set(eo, GCHandle.ToIntPtr(gch)); } - internal static void free_dict_values(Dictionary dict) + internal static void free_dict_values(ConcurrentDictionary dict) { foreach (IntPtr ptr in dict.Values) { @@ -536,7 +537,7 @@ public static class Globals } } - internal static void free_stringshare_values(Dictionary dict) + internal static void free_stringshare_values(ConcurrentDictionary dict) { foreach (IntPtr ptr in dict.Values) { -- cgit v1.2.1