1
Vote

Memory leak on Lost Focus

description

When ChildWindow loses several time focus in ChildWindow_LostFocus method, application subscribe several times on Application.Current.RootVisual.GotFocus.
To improve this simply write next code before Application.Current.RootVisual.GotFocus subscription:
Application.Current.RootVisual.GotFocus -= new RoutedEventHandler(this.RootVisual_GotFocus);
So method ChildWindow_LostFocus will be:
private void ChildWindow_LostFocus(object sender, RoutedEventArgs e)
    {
        if (this.IsOpen && Application.Current != null && Application.Current.RootVisual != null)
        {
            this.InteractionState = WindowInteractionState.BlockedByModalWindow;
            Application.Current.RootVisual.GotFocus -= new RoutedEventHandler(this.RootVisual_GotFocus);
            Application.Current.RootVisual.GotFocus += new RoutedEventHandler(this.RootVisual_GotFocus);
        }
    }

comments