From 6f10775ec42eb1a1340abac569f852437b345c82 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sun, 29 Nov 2015 17:23:28 +0100 Subject: [PATCH] Unsubscribe from RenderTransform.Changed When control is not attached to a visual tree. This fixes a memory leak with TreeView. --- src/Perspex.SceneGraph/Visual.cs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Perspex.SceneGraph/Visual.cs b/src/Perspex.SceneGraph/Visual.cs index 8d164de630..6779cd7269 100644 --- a/src/Perspex.SceneGraph/Visual.cs +++ b/src/Perspex.SceneGraph/Visual.cs @@ -383,8 +383,17 @@ namespace Perspex /// Called when the control is added to a visual tree. /// /// The event args. + /// + /// It is vital that if you override this method you call the base implementation; + /// failing to do so will cause numerous features to not work as expected. + /// protected virtual void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) { + if (RenderTransform != null) + { + RenderTransform.Changed += RenderTransformChanged; + } + AttachedToVisualTree?.Invoke(this, e); } @@ -392,8 +401,17 @@ namespace Perspex /// Called when the control is removed from a visual tree. /// /// The event args. + /// + /// It is vital that if you override this method you call the base implementation; + /// failing to do so will cause numerous features to not work as expected. + /// protected virtual void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) { + if (RenderTransform != null) + { + RenderTransform.Changed -= RenderTransformChanged; + } + DetachedFromVisualTree?.Invoke(this, e); } @@ -475,7 +493,7 @@ namespace Perspex { var sender = e.Sender as Visual; - if (sender != null) + if (sender?._isAttachedToVisualTree == true) { var oldValue = e.OldValue as Transform; var newValue = e.NewValue as Transform;