diff options
author | E.Z. Hart <hartez@users.noreply.github.com> | 2016-09-21 16:04:36 -0600 |
---|---|---|
committer | Rui Marinho <me@ruimarinho.net> | 2016-09-21 23:04:36 +0100 |
commit | 74cb5c4a97dcb123eb471f6b1dffa1267d0305aa (patch) | |
tree | d463831cc17cba1ddfb4ba977cdc6054597ed61a | |
parent | d4efe0a37ea87967e38a7d0325df8d84e1c125fc (diff) | |
download | xamarin-forms-74cb5c4a97dcb123eb471f6b1dffa1267d0305aa.tar.gz xamarin-forms-74cb5c4a97dcb123eb471f6b1dffa1267d0305aa.tar.bz2 xamarin-forms-74cb5c4a97dcb123eb471f6b1dffa1267d0305aa.zip |
Implement dispose pattern correctly to support derived classes (#364)
4 files changed, 59 insertions, 22 deletions
diff --git a/Xamarin.Forms.Platform.Android/VisualElementPackager.cs b/Xamarin.Forms.Platform.Android/VisualElementPackager.cs index e6db1c6a..891d4383 100644 --- a/Xamarin.Forms.Platform.Android/VisualElementPackager.cs +++ b/Xamarin.Forms.Platform.Android/VisualElementPackager.cs @@ -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) diff --git a/Xamarin.Forms.Platform.Android/VisualElementTracker.cs b/Xamarin.Forms.Platform.Android/VisualElementTracker.cs index 2b9815d3..7ade4596 100644 --- a/Xamarin.Forms.Platform.Android/VisualElementTracker.cs +++ b/Xamarin.Forms.Platform.Android/VisualElementTracker.cs @@ -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; + } } } diff --git a/Xamarin.Forms.Platform.WP8/VisualElementTracker.cs b/Xamarin.Forms.Platform.WP8/VisualElementTracker.cs index 21633806..45b51a95 100644 --- a/Xamarin.Forms.Platform.WP8/VisualElementTracker.cs +++ b/Xamarin.Forms.Platform.WP8/VisualElementTracker.cs @@ -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) diff --git a/Xamarin.Forms.Platform.WinRT/VisualElementPackager.cs b/Xamarin.Forms.Platform.WinRT/VisualElementPackager.cs index 686da587..f2e8794d 100644 --- a/Xamarin.Forms.Platform.WinRT/VisualElementPackager.cs +++ b/Xamarin.Forms.Platform.WinRT/VisualElementPackager.cs @@ -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; |