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

Change last seen parent to weak ref #801

Open
wants to merge 3 commits into
base: uwp/main
Choose a base branch
from
Open
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
16 changes: 13 additions & 3 deletions winrt/lib/xaml/BaseControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas { na

RenderTarget m_currentRenderTarget;

ComPtr<IDependencyObject> m_lastSeenParent;
WeakRef m_lastSeenParent;

ComPtr<ICanvasDevice> m_customDevice;

Expand Down Expand Up @@ -233,7 +233,11 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas { na
return ExceptionBoundary(
[&]
{
RemoveFromVisualTreeImpl(m_lastSeenParent.Get(), As<IUIElement>(GetControl()).Get());
auto control = As<IUIElement>(GetControl()); //Do this first because the unit tests expect a failure when this cast fails.
if (!m_lastSeenParent) {
return;
}
RemoveFromVisualTreeImpl(LockWeakRef<IDependencyObject>(m_lastSeenParent).Get(), control.Get());
});
}

Expand Down Expand Up @@ -913,7 +917,13 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas { na

void UpdateLastSeenParent()
{
As<IFrameworkElement>(GetControl()->GetComposableBase())->get_Parent(&m_lastSeenParent);
ComPtr<IDependencyObject> parent;
As<IFrameworkElement>(GetControl()->GetComposableBase())->get_Parent(&parent);
if (!parent) {
m_lastSeenParent.Reset();
return;
}
m_lastSeenParent = AsWeak(parent.Get());
}

HRESULT OnUnloaded(IInspectable*, IRoutedEventArgs*)
Expand Down
14 changes: 12 additions & 2 deletions winrt/lib/xaml/CanvasSwapChainPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,13 @@ HRESULT CanvasSwapChainPanel::OnLoaded(IInspectable*, IRoutedEventArgs*)
return ExceptionBoundary(
[&]
{
As<IFrameworkElement>(GetComposableBase())->get_Parent(&m_lastSeenParent);
ComPtr<IDependencyObject> parent;
As<IFrameworkElement>(GetComposableBase())->get_Parent(&parent);
if (!parent) {
m_lastSeenParent.Reset();
return;
}
m_lastSeenParent = AsWeak(parent.Get());
});
}

Expand Down Expand Up @@ -130,7 +136,11 @@ IFACEMETHODIMP CanvasSwapChainPanel::RemoveFromVisualTree()
return ExceptionBoundary(
[&]
{
RemoveFromVisualTreeImpl(m_lastSeenParent.Get(), As<IUIElement>(this).Get());
auto control = As<IUIElement>(this); //Do this first because the unit tests expect a failure when this cast fails.
if (!m_lastSeenParent) {
return;
}
RemoveFromVisualTreeImpl(LockWeakRef<IDependencyObject>(m_lastSeenParent).Get(), control.Get());
});
}

Expand Down
2 changes: 1 addition & 1 deletion winrt/lib/xaml/CanvasSwapChainPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas { na
std::shared_ptr<ICanvasSwapChainPanelAdapter> m_adapter;

ComPtr<ICanvasSwapChain> m_canvasSwapChain;
ComPtr<IDependencyObject> m_lastSeenParent;
WeakRef m_lastSeenParent;

public:
CanvasSwapChainPanel(std::shared_ptr<ICanvasSwapChainPanelAdapter> adapter);
Expand Down