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?
This commit is contained in:
Lauro Moura 2019-12-04 10:24:00 -03:00
parent 57a1b3a63e
commit 3875c3d949
2 changed files with 11 additions and 3 deletions

View File

@ -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<string, IntPtr> cached_strings = new ConcurrentDictionary<string, IntPtr>();
private ConcurrentDictionary<string, IntPtr> cached_stringshares = new ConcurrentDictionary<string, IntPtr>();
private Hashtable keyValueHash = null;
/// <summary>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);
}
/// <summary>Turns the native pointer into a string representation.

View File

@ -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<String, IntPtr> dict)
internal static void free_dict_values(ConcurrentDictionary<String, IntPtr> dict)
{
foreach (IntPtr ptr in dict.Values)
{
@ -536,7 +537,7 @@ public static class Globals
}
}
internal static void free_stringshare_values(Dictionary<String, IntPtr> dict)
internal static void free_stringshare_values(ConcurrentDictionary<String, IntPtr> dict)
{
foreach (IntPtr ptr in dict.Values)
{