diff options
author | Lauro Moura <lauromoura@expertisesolutions.com.br> | 2017-07-07 17:42:52 -0300 |
---|---|---|
committer | Lauro Moura <lauromoura@expertisesolutions.com.br> | 2017-07-07 19:42:06 -0300 |
commit | 8c73e94cdf4af2a33afa95eeec31495650863c8e (patch) | |
tree | 30795ae2d1e701fed9ccc00b2ef3feeb85bebe01 | |
parent | 0116cf0cdf551c951770ffb2fd31258387125433 (diff) | |
download | efl-8c73e94cdf4af2a33afa95eeec31495650863c8e.tar.gz |
efl_mono: Add DisposableIntPtr
It is a wrapper around IntPtr that calls Marshall.FreeHGlobal on
dispose, so you free them when exiting a block.
This can be useful when dealing with intptrs returned from
marshalling functions, where you don't know if it should be freed or
not (e.g. strings vs ints).
-rw-r--r-- | src/bindings/mono/eina_mono/eina_config.cs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/bindings/mono/eina_mono/eina_config.cs b/src/bindings/mono/eina_mono/eina_config.cs index 38aaf46b2c..754faec203 100644 --- a/src/bindings/mono/eina_mono/eina_config.cs +++ b/src/bindings/mono/eina_mono/eina_config.cs @@ -22,4 +22,42 @@ public class Config { } } + +/// <summary> +/// Wrapper class for pointers that need some cleanup afterwards +/// like strings. +/// </summary> +public class DisposableIntPtr : IDisposable { + + public IntPtr Handle { get; set; } + private bool ShouldFree; + private bool Disposed; + + /// <summary>Wraps a new ptr what will be freed based on the + /// value of shouldFree</summary> + public DisposableIntPtr(IntPtr ptr, bool shouldFree=false) + { + Handle = ptr; + ShouldFree = shouldFree; + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (!Disposed && ShouldFree) { + Marshal.FreeHGlobal(this.Handle); + } + Disposed = true; + } + + ~DisposableIntPtr() + { + Dispose(false); + } +} } |