Skip to content

Commit

Permalink
fix most memory leaks from opening and closing Dynamo Splash Screen (#…
Browse files Browse the repository at this point in the history
…14344) (#14364)

* fixing mem leaks in progress

* fix test and remove todo
  • Loading branch information
mjkkirschner authored Sep 1, 2023
1 parent c4341a6 commit c60bc6c
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 24 deletions.
14 changes: 12 additions & 2 deletions src/DynamoCoreWpf/Views/SplashScreen/SplashScreen.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public DynamoView DynamoView
set
{
dynamoView = value;
if(dynamoView == null)
{
return;
}
viewModel = value.DataContext as DynamoViewModel;
// When view model is closed, we need to close the splash screen if it is displayed.
viewModel.RequestClose += SplashScreenRequestClose;
Expand Down Expand Up @@ -484,8 +488,14 @@ internal void CloseWindow()
// RequestUpdateLoadBarStatus event needs to be unsubscribed when the splash screen window is closed, to avoid populating the info on splash screen.
else
{
this.Close();
viewModel?.Model.ShutDown(false);
Close();
if (viewModel != null)
{
viewModel.RequestClose -= SplashScreenRequestClose;
}

DynamoView?.Close();
DynamoView = null;
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/GraphMetadataViewExtension/GraphMetadataViewExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,12 @@ public override void Closed()

protected virtual void Dispose(bool disposing)
{
viewModel.Dispose();

this.graphMetadataMenuItem.Checked -= MenuItemCheckHandler;
this.graphMetadataMenuItem.Unchecked -= MenuItemUnCheckedHandler;
viewModel?.Dispose();
if (graphMetadataMenuItem != null)
{
graphMetadataMenuItem.Checked -= MenuItemCheckHandler;
graphMetadataMenuItem.Unchecked -= MenuItemUnCheckedHandler;
}
}

public override void Dispose()
Expand Down
16 changes: 12 additions & 4 deletions src/LintingViewExtension/LintingViewExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,18 @@ public override void Shutdown()

public override void Dispose()
{
this.linterMenuItem.Checked -= MenuItemCheckHandler;
this.linterMenuItem.Unchecked -= MenuItemUnCheckedHandler;
if (linterManager != null) linterManager.PropertyChanged -= OnLinterManagerPropertyChange;
viewLoadedParamsReference.ViewExtensionOpenRequest -= OnViewExtensionOpenRequest;
if (linterMenuItem != null)
{
linterMenuItem.Checked -= MenuItemCheckHandler;
linterMenuItem.Unchecked -= MenuItemUnCheckedHandler;
}
if (linterManager != null){
linterManager.PropertyChanged -= OnLinterManagerPropertyChange;
}
if (viewLoadedParamsReference != null)
{
viewLoadedParamsReference.ViewExtensionOpenRequest -= OnViewExtensionOpenRequest;
}
}

public override void Closed()
Expand Down
25 changes: 17 additions & 8 deletions src/Notifications/NotificationsViewExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public ObservableCollection<Logging.NotificationMessage> Notifications
get { return notifications; }
private set
{
if(notifications != value)
if (notifications != value)
notifications = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Notifications)));
}
Expand Down Expand Up @@ -62,17 +62,26 @@ public void Dispose()
{
UnregisterEventHandlers();
//for some reason the menuItem was not being gc'd in tests without manually removing it
viewLoadedParams.dynamoMenu.Items.Remove(notificationsMenuItem.MenuItem);
BindingOperations.ClearAllBindings(notificationsMenuItem.CountLabel);
viewLoadedParams?.dynamoMenu.Items.Remove(notificationsMenuItem.MenuItem);
if (notificationsMenuItem != null)
{
BindingOperations.ClearAllBindings(notificationsMenuItem.CountLabel);
}
notificationsMenuItem = null;
disposed = true;
}
}

private void UnregisterEventHandlers()
{
viewLoadedParams.NotificationRecieved -= notificationHandler;
Notifications.CollectionChanged -= notificationsMenuItem.NotificationsChangeHandler;
if (viewLoadedParams != null)
{
viewLoadedParams.NotificationRecieved -= notificationHandler;
}
if (notificationsMenuItem != null)
{
Notifications.CollectionChanged -= notificationsMenuItem.NotificationsChangeHandler;
}
}

public void Loaded(ViewLoadedParams viewStartupParams)
Expand All @@ -83,7 +92,7 @@ public void Loaded(ViewLoadedParams viewStartupParams)
logger = viewModel.Model.Logger;

Notifications = new ObservableCollection<Logging.NotificationMessage>();

notificationHandler = (notificationMessage) =>
{
Notifications.Add(notificationMessage);
Expand Down Expand Up @@ -117,12 +126,12 @@ internal void AddNotifications()

public void Shutdown()
{
// Do nothing for now
// Do nothing for now
}

public void Startup(ViewStartupParams viewStartupParams)
{
// Do nothing for now
// Do nothing for now
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ private void DataContext_Closed(object sender, EventArgs e)
public override void Dispose()
{
PackageDetailsViewModel?.Dispose();
ViewLoadedParamsReference.ViewExtensionOpenRequestWithParameter -= OnViewExtensionOpenWithParameterRequest;
if (ViewLoadedParamsReference != null)
{
ViewLoadedParamsReference.ViewExtensionOpenRequestWithParameter -= OnViewExtensionOpenWithParameterRequest;
}
}

public override void Closed()
Expand Down
15 changes: 11 additions & 4 deletions src/PythonMigrationViewExtension/PythonMigrationViewExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,17 @@ private void UnSubscribeWorkspaceEvents()

private void UnsubscribeEvents()
{
LoadedParams.CurrentWorkspaceChanged -= OnCurrentWorkspaceChanged;
DynamoViewModel.CurrentSpaceViewModel.Model.NodeAdded -= OnNodeAdded;
DynamoViewModel.Model.Logger.NotificationLogged -= OnNotificationLogged;
CurrentWorkspace.RequestPackageDependencies -= PythonDependencies.AddPythonPackageDependency;
if (LoadedParams != null)
{
LoadedParams.CurrentWorkspaceChanged -= OnCurrentWorkspaceChanged;
DynamoViewModel.CurrentSpaceViewModel.Model.NodeAdded -= OnNodeAdded;
DynamoViewModel.Model.Logger.NotificationLogged -= OnNotificationLogged;
}

if (CurrentWorkspace != null)
{
CurrentWorkspace.RequestPackageDependencies -= PythonDependencies.AddPythonPackageDependency;
}
UnSubscribeWorkspaceEvents();
}
#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public override string UniqueId
/// </summary>
public override void Dispose()
{
DependencyView.Dispose();
DependencyView?.Dispose();
dataRows = null;
localDefinitionDataRows = null;
externalFilesDataRows = null;
Expand Down

0 comments on commit c60bc6c

Please sign in to comment.