mono: implement dispose method based on dispose pattern
authorYeongjong Lee <yj34.lee@samsung.com>
Thu, 24 Oct 2019 20:46:13 +0000 (17:46 -0300)
committerWonki Kim <wonki_.kim@samsung.com>
Mon, 11 Nov 2019 02:20:38 +0000 (11:20 +0900)
Summary:
Fix CA1063, CA1816
ref T8400, T8419

Test Plan:
meson setup -Dbindings=mono,cxx -Dmono-beta=true
ninja test

Reviewers: felipealmeida, brunobelo, YOhoho

Reviewed By: YOhoho

Subscribers: cedric, #reviewers, #committers

Tags: #efl

Maniphest Tasks: T8419, T8400

Differential Revision: https://phab.enlightenment.org/D10460

src/bindings/mono/efl_mono/GenericModel.cs
src/bindings/mono/efl_mono/ItemFactory.cs
src/bindings/mono/efl_mono/UserModel.cs
src/bindings/mono/eina_mono/eina_accessor.cs
src/bindings/mono/eo_mono/NativeModule.cs

index 1f92b8c..8bfa8ce 100644 (file)
@@ -18,7 +18,7 @@ namespace Efl {
 /// </summary>
 /// <typeparam name="T">The type of the child model. It is the type used when adding/removing/getting items to this
 /// model.</typeparam>
-public class GenericModel<T> : Efl.Object, Efl.IModel, IDisposable
+public class GenericModel<T> : Efl.Object, Efl.IModel
 {
    private Efl.IModel model;
 
index 34791e1..a97401c 100644 (file)
@@ -16,7 +16,7 @@ namespace Efl.Ui
 /// factory.Style().Bind("Name"); // The factory Style property is bound to the Name property for the given model.
 /// </code>
 /// </summary>
-public class ItemFactory<T> : Efl.Ui.LayoutFactory, IDisposable
+public class ItemFactory<T> : Efl.Ui.LayoutFactory
 {
     /// <summary>Creates a new factory.
     /// </summary>
index 1116dfc..a235a3a 100644 (file)
@@ -67,7 +67,7 @@ internal class ModelHelper
 /// </summary>
 /// <typeparam name="T">The enclosed C# model class with the properties to be added to the native model.</typeparam>
 [Efl.Eo.BindingEntity]
-public class UserModel<T> : Efl.MonoModelInternal, IDisposable
+public class UserModel<T> : Efl.MonoModelInternal
 {
    /// <summary>
    /// Creates a new root model.
@@ -85,12 +85,6 @@ public class UserModel<T> : Efl.MonoModelInternal, IDisposable
      }
    }
 
-   /// <summary>Disposes of this instance.</summary>
-   ~UserModel()
-   {
-       Dispose(false);
-   }
-
    /// <summary>Adds a new child to the model wrapping the properties of <c>o</c>
    ///
    /// <para>Reflection is used to instantiate a new <see cref="Efl.IModel" />-based class for this child and
index 6c939bc..64b4216 100644 (file)
@@ -95,6 +95,7 @@ public class Accessor<T> : IEnumerable<T>, IDisposable
     public void Dispose()
     {
         Dispose(true);
+        GC.SuppressFinalize(this);
     }
 
     /// <summary>Disposes of this wrapper, releasing the native accessor if
index fd3b315..ad9cc60 100644 (file)
@@ -22,6 +22,7 @@ namespace Efl.Eo
 public partial class NativeModule : IDisposable
 {
     private Lazy<IntPtr> module;
+    private bool disposed = false;
 
     ///<summary>Lazily tries to load the module with the given name.</summary>
     ///<param name="libName">The name of the module to load.</param>
@@ -43,12 +44,39 @@ public partial class NativeModule : IDisposable
         }
     }
 
-    ///<summary>Unload and released the handle to the wrapped module.</summary>
+    /// <summary>Finalizer to be called from the Garbage Collector.</summary>
+    ~NativeModule()
+    {
+        Dispose(false);
+    }
+
+    /// <summary>Unload and released the handle to the wrapped module.</summary>
     public void Dispose()
     {
-        UnloadLibrary(module.Value);
-        module = null;
+        Dispose(true);
+        GC.SuppressFinalize(this);
     }
+
+    /// <summary>Unload and released the handle to the wrapped module.</summary>
+    protected virtual void Dispose(bool disposing)
+    {
+        if (disposed)
+        {
+            return;
+        }
+
+        if (disposing)
+        {
+           module = null;
+        }
+
+        if (module.IsValueCreated)
+        {
+           UnloadLibrary(module.Value);
+        }
+
+        disposed = true;
+   }
 }
 
 }