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 issue with exception being thrown during cleanup of NavigationView. #6797

Merged
merged 3 commits into from
Mar 12, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 18 additions & 2 deletions dev/NavigationView/NavigationView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3418,19 +3418,35 @@ void NavigationView::SetNavigationViewItemRevokers(const winrt::NavigationViewIt

void NavigationView::ClearNavigationViewItemRevokers(const winrt::NavigationViewItem& nvi)
{
RevokeNavigationViewItemRevokers(nvi);
nvi.SetValue(s_NavigationViewItemRevokersProperty, nullptr);
m_itemsWithRevokerObjects.erase(nvi);
}

void NavigationView::ClearAllNavigationViewItemRevokers()
void NavigationView::ClearAllNavigationViewItemRevokers() noexcept
{
for (const auto& nvi : m_itemsWithRevokerObjects)
{
nvi.SetValue(s_NavigationViewItemRevokersProperty, nullptr);
try
Copy link
Member

Choose a reason for hiding this comment

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

Add a comment explaining why this is necessary.

{
RevokeNavigationViewItemRevokers(nvi);
nvi.SetValue(s_NavigationViewItemRevokersProperty, nullptr);
}
catch (...) {}
Copy link
Member

Choose a reason for hiding this comment

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

Do we know what was causing the exceptions in the first place?

Copy link
Member

Choose a reason for hiding this comment

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

I'm currently looking into it, because I'm now able to reproduce the issue. 🙂

}
m_itemsWithRevokerObjects.clear();
}

void NavigationView::RevokeNavigationViewItemRevokers(const winrt::NavigationViewItem& nvi)
{
if (auto const revokers = nvi.GetValue(s_NavigationViewItemRevokersProperty))
{
if (auto const revokersAsNVIR = revokers.try_as<NavigationViewItemRevokers>()) {
revokersAsNVIR->RevokeAll();
}
}
}

void NavigationView::InvalidateTopNavPrimaryLayout()
{
if (m_appliedTemplate && IsTopNavigationView())
Expand Down
3 changes: 2 additions & 1 deletion dev/NavigationView/NavigationView.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ class NavigationView :
inline static GlobalDependencyProperty s_NavigationViewItemRevokersProperty{ nullptr };
void SetNavigationViewItemRevokers(const winrt::NavigationViewItem& nvi);
void ClearNavigationViewItemRevokers(const winrt::NavigationViewItem& nvi);
void ClearAllNavigationViewItemRevokers();
void ClearAllNavigationViewItemRevokers() noexcept;
void RevokeNavigationViewItemRevokers(const winrt::NavigationViewItem& nvi);
std::set<winrt::NavigationViewItem> m_itemsWithRevokerObjects;

void InvalidateTopNavPrimaryLayout();
Expand Down
8 changes: 8 additions & 0 deletions dev/NavigationView/NavigationViewItemRevokers.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@ class NavigationViewItemRevokers : public winrt::implements<NavigationViewItemRe
winrt::UIElement::GotFocus_revoker gotFocusRevoker{};
PropertyChanged_revoker isSelectedRevoker{};
PropertyChanged_revoker isExpandedRevoker{};

void RevokeAll() {
if (tappedRevoker) tappedRevoker.revoke();
if (keyDownRevoker) keyDownRevoker.revoke();
if (gotFocusRevoker) gotFocusRevoker.revoke();
if (isSelectedRevoker) isSelectedRevoker.revoke();
if (isExpandedRevoker) isExpandedRevoker.revoke();
}
};
34 changes: 34 additions & 0 deletions dev/NavigationView/NavigationView_ApiTests/NavigationViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1263,5 +1263,39 @@ void SetPaneConfigAndVerifyToolTips(NavigationViewPaneDisplayMode paneDisplayMod
}
});
}

[TestMethod]
public void VerifyNVIOutlivingNVDoesNotCrash()
{
NavigationViewItem menuItem1 = null;
RunOnUIThread.Execute(() =>
{
var navView = new NavigationView();
menuItem1 = new NavigationViewItem();

navView.MenuItems.Add(menuItem1);
Content = navView;
Content.UpdateLayout();

navView.MenuItems.Clear();
Content = menuItem1;
Content.UpdateLayout();
});

IdleSynchronizer.Wait();

RunOnUIThread.Execute(() =>
{
GC.Collect();
});

IdleSynchronizer.Wait();

RunOnUIThread.Execute(() =>
{
// NavigationView has a handler on NVI's IsSelected DependencyPropertyChangedEvent.
menuItem1.IsSelected = !menuItem1.IsSelected;
});
}
}
}