summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLauro Moura <lauromoura@expertisesolutions.com.br>2017-07-07 17:42:52 -0300
committerFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2017-07-13 19:00:54 -0300
commit206c904c140311e8fe164ff24549cce0a52bfa90 (patch)
tree173482cc247dd3b13b175c6df6e8b1dea66f3d56
parent44850a8dbf667a4901639c010e3ebe6f85951669 (diff)
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.cs38
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 {
22 } 22 }
23 23
24} 24}
25
26/// <summary>
27/// Wrapper class for pointers that need some cleanup afterwards
28/// like strings.
29/// </summary>
30public class DisposableIntPtr : IDisposable {
31
32 public IntPtr Handle { get; set; }
33 private bool ShouldFree;
34 private bool Disposed;
35
36 /// <summary>Wraps a new ptr what will be freed based on the
37 /// value of shouldFree</summary>
38 public DisposableIntPtr(IntPtr ptr, bool shouldFree=false)
39 {
40 Handle = ptr;
41 ShouldFree = shouldFree;
42 }
43
44 public void Dispose()
45 {
46 Dispose(true);
47 GC.SuppressFinalize(this);
48 }
49
50 protected virtual void Dispose(bool disposing)
51 {
52 if (!Disposed && ShouldFree) {
53 Marshal.FreeHGlobal(this.Handle);
54 }
55 Disposed = true;
56 }
57
58 ~DisposableIntPtr()
59 {
60 Dispose(false);
61 }
62}
25} 63}