Implement dispose pattern correctly to support derived classes (#364)
authorE.Z. Hart <hartez@users.noreply.github.com>
Wed, 21 Sep 2016 22:04:36 +0000 (16:04 -0600)
committerRui Marinho <me@ruimarinho.net>
Wed, 21 Sep 2016 22:04:36 +0000 (23:04 +0100)
Xamarin.Forms.Platform.Android/VisualElementPackager.cs
Xamarin.Forms.Platform.Android/VisualElementTracker.cs
Xamarin.Forms.Platform.WP8/VisualElementTracker.cs
Xamarin.Forms.Platform.WinRT/VisualElementPackager.cs

index e6db1c6..891d438 100644 (file)
@@ -33,8 +33,15 @@ namespace Xamarin.Forms.Platform.Android
 
                public void Dispose()
                {
+                       Dispose(true);
+                       GC.SuppressFinalize(this);
+               }
+
+               protected virtual void Dispose(bool disposing)
+               {
                        if (_disposed)
                                return;
+
                        _disposed = true;
 
                        if (_renderer != null)
index 2b9815d..7ade459 100644 (file)
@@ -45,18 +45,28 @@ namespace Xamarin.Forms.Platform.Android
 
                public void Dispose()
                {
+                       Dispose(true);
+                       GC.SuppressFinalize(this);
+               }
+
+               protected virtual void Dispose(bool disposing)
+               {
                        if (_disposed)
                                return;
-                       _disposed = true;
 
-                       SetElement(_element, null);
+                       _disposed = true;
 
-                       if (_renderer != null)
+                       if (disposing)
                        {
-                               _renderer.ElementChanged -= RendererOnElementChanged;
-                               _renderer.ViewGroup.RemoveOnAttachStateChangeListener(AttachTracker.Instance);
-                               _renderer = null;
-                               _context = null;
+                               SetElement(_element, null);
+
+                               if (_renderer != null)
+                               {
+                                       _renderer.ElementChanged -= RendererOnElementChanged;
+                                       _renderer.ViewGroup.RemoveOnAttachStateChangeListener(AttachTracker.Instance);
+                                       _renderer = null;
+                                       _context = null;
+                               }
                        }
                }
 
index 2163380..45b51a9 100644 (file)
@@ -13,7 +13,15 @@ namespace Xamarin.Forms.Platform.WinPhone
        {
                public abstract FrameworkElement Child { get; set; }
 
-               public abstract void Dispose();
+               public void Dispose()
+               {
+                       Dispose(true);
+                       GC.SuppressFinalize(this);
+               }
+
+               protected virtual void Dispose(bool disposing)
+               {
+               }
 
                public event EventHandler Updated;
 
@@ -106,29 +114,35 @@ namespace Xamarin.Forms.Platform.WinPhone
                        }
                }
 
-               public override void Dispose()
+               protected override void Dispose(bool disposing)
                {
                        if (_disposed)
                                return;
+
                        _disposed = true;
 
-                       if (_element != null)
+                       if (disposing)
                        {
-                               _element.Tap -= ElementOnTap;
-                               _element.DoubleTap -= ElementOnDoubleTap;
-                               _element.ManipulationDelta -= OnManipulationDelta;
-                               _element.ManipulationCompleted -= OnManipulationCompleted;
-                       }
+                               if (_element != null)
+                               {
+                                       _element.Tap -= ElementOnTap;
+                                       _element.DoubleTap -= ElementOnDoubleTap;
+                                       _element.ManipulationDelta -= OnManipulationDelta;
+                                       _element.ManipulationCompleted -= OnManipulationCompleted;
+                               }
 
-                       if (_model != null)
-                       {
-                               _model.BatchCommitted -= HandleRedrawNeeded;
-                               _model.PropertyChanged -= HandlePropertyChanged;
+                               if (_model != null)
+                               {
+                                       _model.BatchCommitted -= HandleRedrawNeeded;
+                                       _model.PropertyChanged -= HandlePropertyChanged;
+                               }
+
+                               Child = null;
+                               Model = null;
+                               Element = null;
                        }
 
-                       Child = null;
-                       Model = null;
-                       Element = null;
+                       base.Dispose(disposing);
                }
 
                protected virtual void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
index 686da58..f2e8794 100644 (file)
@@ -46,6 +46,12 @@ namespace Xamarin.Forms.Platform.WinRT
 
                public void Dispose()
                {
+                       Dispose(true);
+                       GC.SuppressFinalize(this);
+               }
+
+               protected virtual void Dispose(bool disposing)
+               {
                        if (_disposed)
                                return;