Skip to content

Commit

Permalink
dispose methods null checks
Browse files Browse the repository at this point in the history
- trying to prevent null exceptions in case resources have been cleared before dispose event has been called
  • Loading branch information
dnenov committed Nov 28, 2023
1 parent e43ad53 commit f5d5563
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,7 @@ internal void AddToSearchResults(PackageManagerSearchElementViewModel element)

internal void ClearSearchResults()
{
if (this.SearchResults == null) return;
foreach (var ele in this.SearchResults)
{
ele.RequestDownload -= PackageOnExecuted;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ private void InitializeInstalledPackages()
/// </summary>
public void Dispose()
{
if (LocalPackages == null) return;

LocalPackages.CollectionChanged -= LocalPackages_CollectionChanged;
}
}
Expand Down
36 changes: 25 additions & 11 deletions src/DynamoCoreWpf/ViewModels/Search/BrowserItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ internal static BrowserInternalElementViewModel WrapExplicit(BrowserInternalElem

public void Dispose()
{
if (Model == null || Model.Items == null) return;
Model.Items.CollectionChanged -= ItemsOnCollectionChanged;
}

Expand Down Expand Up @@ -369,15 +370,22 @@ private void SubCategoriesOnCollectionChanged(object sender, NotifyCollectionCha
/// </summary>
public override void Dispose()
{
foreach (var category in SubCategories)
category.PropertyChanged -= CategoryOnPropertyChanged;
if (SubCategories != null)
{
foreach (var category in SubCategories)
category.PropertyChanged -= CategoryOnPropertyChanged;
}

foreach (var item in Items)
item.PropertyChanged -= ItemOnPropertyChanged;
if (Items != null)
{
foreach (var item in Items)
item.PropertyChanged -= ItemOnPropertyChanged;
Items.CollectionChanged -= ItemsOnCollectionChanged;
}

if (Entries != null) Entries.CollectionChanged -= OnCollectionChanged;
if (SubCategories != null) SubCategories.CollectionChanged -= SubCategoriesOnCollectionChanged;

Entries.CollectionChanged -= OnCollectionChanged;
SubCategories.CollectionChanged -= SubCategoriesOnCollectionChanged;
Items.CollectionChanged -= ItemsOnCollectionChanged;
base.Dispose();
}

Expand All @@ -389,11 +397,17 @@ public void DisposeTree()
{
Dispose();

foreach (var entry in Entries)
entry.Dispose();
if (Entries != null)
{
foreach (var entry in Entries)
entry.Dispose();
}

foreach (var subCategory in SubCategories)
subCategory.DisposeTree();
if (SubCategories != null)
{
foreach (var subCategory in SubCategories)
subCategory.DisposeTree();
}
}

private void CategoryOnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
Expand Down
1 change: 1 addition & 0 deletions src/DynamoCoreWpf/Views/Core/DynamoView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2030,6 +2030,7 @@ private void WindowClosed(object sender, EventArgs e)
this.Dispose();
sharedViewExtensionLoadedParams?.Dispose();
this._pkgSearchVM?.Dispose();
this._pkgVM?.Dispose();
}

// the key press event is being intercepted before it can get to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ public void Dispose()
Actions.Close,
Categories.PackageManagerOperations);

PublishPackageViewModel.PublishSuccess -= PackageViewModelOnPublishSuccess;
PublishPackageViewModel.RequestShowFolderBrowserDialog -= OnRequestShowFolderBrowserDialog;
if(PublishPackageViewModel != null )
{
PublishPackageViewModel.PublishSuccess -= PackageViewModelOnPublishSuccess;
PublishPackageViewModel.RequestShowFolderBrowserDialog -= OnRequestShowFolderBrowserDialog;

}

this.Loaded -= InitializeContext;

Expand All @@ -73,7 +77,7 @@ public void Dispose()
PublishPages = null;
NavButtonStacks = null;

Breadcrumbs.Clear();
Breadcrumbs?.Clear();
}

private void InitializePages()
Expand Down Expand Up @@ -101,6 +105,8 @@ private void InitializePages()

private void PackageViewModelOnPublishSuccess(PublishPackageViewModel sender)
{
if (PublishPages == null) return;

statusLabel.Visibility = Visibility.Collapsed;

currentPage = 3;
Expand Down Expand Up @@ -270,6 +276,8 @@ private void ToggleButtonRowVisibility(int page)

private void DisposePages()
{
if (PublishPages == null || !PublishPages.Any()) return;

foreach(var page in PublishPages.Values)
{
if (page is PublishPackagePublishPage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,10 @@ private void CloseButton_Click(object sender, RoutedEventArgs e)

private void WindowClosed(object sender, EventArgs e)
{
this.packageManagerPublish.Dispose();
this.packageManagerSearch.Dispose();
this.packageManagerPublish?.Dispose();
this.packageManagerSearch?.Dispose();

if (PackageManagerViewModel == null) return;
this.PackageManagerViewModel.PackageSearchViewModel.RequestShowFileDialog -= OnRequestShowFileDialog;
this.PackageManagerViewModel.PackageSearchViewModel.PackageManagerViewClose();
this.PackageManagerViewModel.PublishPackageViewModel.CancelCommand.Execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void Dispose()
{
this.PublishPackageViewModel = null;
this.DataContextChanged -= PublishPackagePublishPage_DataContextChanged;
this.customBrowserControl.Dispose();
this.customBrowserControl?.Dispose();
}

private void customBrowserControl_Loaded(object sender, RoutedEventArgs e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void Dispose()
{
this.PublishPackageViewModel = null;
this.DataContextChanged -= PublishPackagePublishPage_DataContextChanged;
this.previewBrowserControl.Dispose();
this.previewBrowserControl?.Dispose();
}

private void NavigationButton_Click(object sender, RoutedEventArgs e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ internal void LoadEvents()

public void Dispose()
{
this.ItemSelection.Clear();
this.ItemSelection?.Clear();
this.PublishPackageViewModel = null;
this.DataContextChanged -= PublishPackagePublishPage_DataContextChanged;
this.customBrowserControl.Dispose();
this.customBrowserControl?.Dispose();
}

private void customBrowserControl_Loaded(object sender, RoutedEventArgs e)
Expand Down
4 changes: 2 additions & 2 deletions src/DynamoPackages/PackageDirectoryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public IDirectoryInfo BuildDirectory(Package package, string packagesDirectory,
WritePackageHeader(package, rootDir);
RemoveUnselectedFiles(contentFiles, rootDir);
CopyFilesIntoPackageDirectory(contentFiles, markdownFiles, dyfDir, binDir, extraDir, docDir);
RemoveDyfFiles(contentFiles, dyfDir); // Why do we need to remove the dyf files from the publishing folder?
RemoveDyfFiles(contentFiles, dyfDir);

RemapCustomNodeFilePaths(contentFiles, dyfDir.FullName);

Expand All @@ -77,7 +77,7 @@ public IDirectoryInfo BuildRetainDirectory(Package package, string packagesDirec

RemoveUnselectedFiles(contentFiles.SelectMany(files => files).ToList(), rootDir);
CopyFilesIntoRetainedPackageDirectory(contentFiles, markdownFiles, rootDir, out dyfFiles);
RemoveRetainDyfFiles(contentFiles.SelectMany(files => files).ToList(), dyfFiles); // Why do we need to remove the dyf files from the publishing folder?
RemoveRetainDyfFiles(contentFiles.SelectMany(files => files).ToList(), dyfFiles);

RemapRetainCustomNodeFilePaths(contentFiles.SelectMany(files => files).ToList(), dyfFiles);

Expand Down

0 comments on commit f5d5563

Please sign in to comment.