Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Dynamo crash after long idle #11537

Merged
merged 3 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions src/DynamoCoreWpf/Views/Core/DynamoView.xaml
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
<Window x:Class="Dynamo.Controls.DynamoView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:views="clr-namespace:Dynamo.Views"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:configuration="clr-namespace:Dynamo.Configuration;assembly=DynamoCore"
xmlns:controls="clr-namespace:Dynamo.Controls"
xmlns:workspaces="clr-namespace:Dynamo.Graph.Workspaces;assembly=DynamoCore"
xmlns:ui="clr-namespace:Dynamo.UI"
xmlns:uictrls="clr-namespace:Dynamo.UI.Controls"
xmlns:service="clr-namespace:Dynamo.Services"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:viewModels="clr-namespace:Dynamo.ViewModels"
xmlns:views="clr-namespace:Dynamo.Views"
xmlns:controls1="clr-namespace:Dynamo.Wpf.Controls"
xmlns:p="clr-namespace:Dynamo.Wpf.Properties;assembly=DynamoCoreWpf"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
d:DataContext="{d:DesignInstance viewModels:DynamoViewModel, IsDesignTimeCreatable=False}"
mc:Ignorable="d"
xmlns:ui="clr-namespace:Dynamo.UI"
xmlns:p="clr-namespace:Dynamo.Wpf.Properties;assembly=DynamoCoreWpf"
xmlns:controls1="clr-namespace:Dynamo.Wpf.Controls"
xmlns:configuration="clr-namespace:Dynamo.Configuration;assembly=DynamoCore"
xmlns:workspaces="clr-namespace:Dynamo.Graph.Workspaces;assembly=DynamoCore"
x:Name="_this"
Height="768"
Width="1024"
Expand Down
82 changes: 34 additions & 48 deletions src/DynamoCoreWpf/Views/Core/WorkspaceView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ internal bool IsSnappedToPort
}
}

/// <summary>
/// Constructor
/// </summary>
public WorkspaceView()
{
Resources.MergedDictionaries.Add(SharedDictionaryManager.DynamoModernDictionary);
Expand All @@ -92,9 +95,6 @@ public WorkspaceView()

DataContextChanged += OnWorkspaceViewDataContextChanged;

Loaded += OnWorkspaceViewLoaded;
Unloaded += OnWorkspaceViewUnloaded;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The OnWorkspaceViewDataContextChanged will be triggered in both cases so I do not see them as must have.


// view of items to drag
draggedSelectionTemplate = (DataTemplate)FindResource("DraggedSelectionTemplate");
var dictionaries = draggedSelectionTemplate.Resources.MergedDictionaries;
Expand All @@ -104,16 +104,6 @@ public WorkspaceView()
dictionaries.Add(SharedDictionaryManager.DataTemplatesDictionary);
}

void OnWorkspaceViewLoaded(object sender, RoutedEventArgs e)
{
DynamoSelection.Instance.Selection.CollectionChanged += OnSelectionCollectionChanged;

ViewModel.RequestShowInCanvasSearch += ShowHideInCanvasControl;
ViewModel.DynamoViewModel.PropertyChanged += ViewModel_PropertyChanged;

infiniteGridView.AttachToZoomBorder(zoomBorder);
}

void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
Expand All @@ -128,9 +118,14 @@ void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
}
}

private void removeViewModelsubscriptions(WorkspaceViewModel ViewModel)
/// <summary>
/// clean up view model subscriptions to prevent memory leak
/// </summary>
/// <param name="ViewModel"></param>
private void RemoveViewModelsubscriptions(WorkspaceViewModel ViewModel)
{
ViewModel.RequestShowInCanvasSearch -= ShowHideInCanvasControl;
ViewModel.RequestNodeAutoCompleteSearch -= ShowHideNodeAutoCompleteControl;
ViewModel.DynamoViewModel.PropertyChanged -= ViewModel_PropertyChanged;

ViewModel.ZoomChanged -= vm_ZoomChanged;
Expand All @@ -143,22 +138,36 @@ private void removeViewModelsubscriptions(WorkspaceViewModel ViewModel)
ViewModel.WorkspacePropertyEditRequested -= VmOnWorkspacePropertyEditRequested;
ViewModel.RequestSelectionBoxUpdate -= VmOnRequestSelectionBoxUpdate;

ViewModel.Model.RequestNodeCentered -= vm_RequestNodeCentered;
ViewModel.Model.RequestNodeCentered -= vm_RequestNodeCentered;
ViewModel.Model.CurrentOffsetChanged -= vm_CurrentOffsetChanged;
DynamoSelection.Instance.Selection.CollectionChanged -= OnSelectionCollectionChanged;
infiniteGridView.DetachFromZoomBorder(zoomBorder);
}

void OnWorkspaceViewUnloaded(object sender, RoutedEventArgs e)
/// <summary>
/// Attach view model subscriptions
/// </summary>
/// <param name="ViewModel"></param>
private void AttachViewModelsubscriptions(WorkspaceViewModel ViewModel)
{
DynamoSelection.Instance.Selection.CollectionChanged -= OnSelectionCollectionChanged;
ViewModel.RequestShowInCanvasSearch += ShowHideInCanvasControl;
ViewModel.RequestNodeAutoCompleteSearch += ShowHideNodeAutoCompleteControl;
ViewModel.DynamoViewModel.PropertyChanged += ViewModel_PropertyChanged;

if (ViewModel != null)
{
removeViewModelsubscriptions(ViewModel);
}
ViewModel.ZoomChanged += vm_ZoomChanged;
ViewModel.RequestZoomToViewportCenter += vm_ZoomAtViewportCenter;
ViewModel.RequestZoomToViewportPoint += vm_ZoomAtViewportPoint;
ViewModel.RequestZoomToFitView += vm_ZoomToFitView;
ViewModel.RequestCenterViewOnElement += CenterViewOnElement;

infiniteGridView.DetachFromZoomBorder(zoomBorder);

ViewModel.RequestAddViewToOuterCanvas += vm_RequestAddViewToOuterCanvas;
ViewModel.WorkspacePropertyEditRequested += VmOnWorkspacePropertyEditRequested;
ViewModel.RequestSelectionBoxUpdate += VmOnRequestSelectionBoxUpdate;

ViewModel.Model.RequestNodeCentered += vm_RequestNodeCentered;
ViewModel.Model.CurrentOffsetChanged += vm_CurrentOffsetChanged;
DynamoSelection.Instance.Selection.CollectionChanged += OnSelectionCollectionChanged;
infiniteGridView.AttachToZoomBorder(zoomBorder);
}

private void ShowHideNodeAutoCompleteControl(ShowHideFlags flag)
Expand Down Expand Up @@ -365,36 +374,13 @@ void OnWorkspaceViewDataContextChanged(object sender, DependencyPropertyChangedE
if (e.OldValue != null)
{
WorkspaceViewModel oldViewModel = (WorkspaceViewModel)e.OldValue;

oldViewModel.Model.CurrentOffsetChanged -= vm_CurrentOffsetChanged;
oldViewModel.ZoomChanged -= vm_ZoomChanged;
oldViewModel.RequestZoomToViewportCenter -= vm_ZoomAtViewportCenter;
oldViewModel.RequestZoomToViewportPoint -= vm_ZoomAtViewportPoint;
oldViewModel.RequestZoomToFitView -= vm_ZoomToFitView;
oldViewModel.RequestCenterViewOnElement -= CenterViewOnElement;
oldViewModel.Model.RequestNodeCentered -= vm_RequestNodeCentered;
oldViewModel.RequestAddViewToOuterCanvas -= vm_RequestAddViewToOuterCanvas;
oldViewModel.WorkspacePropertyEditRequested -= VmOnWorkspacePropertyEditRequested;
oldViewModel.RequestSelectionBoxUpdate -= VmOnRequestSelectionBoxUpdate;
oldViewModel.RequestNodeAutoCompleteSearch -= ShowHideNodeAutoCompleteControl;
removeViewModelsubscriptions(oldViewModel);
RemoveViewModelsubscriptions(oldViewModel);
}

if (ViewModel != null)
{
// Adding registration of event listener
ViewModel.Model.CurrentOffsetChanged += vm_CurrentOffsetChanged;
ViewModel.ZoomChanged +=vm_ZoomChanged;
ViewModel.RequestZoomToViewportCenter += vm_ZoomAtViewportCenter;
ViewModel.RequestZoomToViewportPoint += vm_ZoomAtViewportPoint;
ViewModel.RequestZoomToFitView += vm_ZoomToFitView;
ViewModel.RequestCenterViewOnElement += CenterViewOnElement;
ViewModel.Model.RequestNodeCentered += vm_RequestNodeCentered;
ViewModel.RequestAddViewToOuterCanvas += vm_RequestAddViewToOuterCanvas;
ViewModel.WorkspacePropertyEditRequested += VmOnWorkspacePropertyEditRequested;
ViewModel.RequestSelectionBoxUpdate += VmOnRequestSelectionBoxUpdate;
ViewModel.RequestNodeAutoCompleteSearch += ShowHideNodeAutoCompleteControl;

AttachViewModelsubscriptions(ViewModel);
ViewModel.Loaded();
}
}
Expand Down