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

[UWP] Fix circular reference in ImageOpened lambda for Auto sized image handling #3507

Merged
merged 2 commits into from
Oct 9, 2019
Merged
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
63 changes: 48 additions & 15 deletions source/uwp/Renderer/lib/AdaptiveImageRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -697,17 +697,33 @@ namespace AdaptiveNamespace
THROW_IF_FAILED(ellipseAsUIElement->put_Visibility(Visibility::Visibility_Collapsed));
// Handle ImageOpened event so we can check the imageSource's size to determine if it fits in its parent
EventRegistrationToken eventToken;
ComPtr<IInspectable> strongParentElement(parentElement);
ComPtr<IInspectable> localParentElement(parentElement);

// Take weak references to the ellipse and parent to avoid circular references between this lambda and
// its parents (Parent->Ellipse->ImageBrush->Lambda->(Parent and Ellipse))
WeakRef weakParent;
THROW_IF_FAILED(localParentElement.AsWeak(&weakParent));

WeakRef weakEllipse;
THROW_IF_FAILED(ellipseAsUIElement.AsWeak(&weakEllipse));
THROW_IF_FAILED(brushAsImageBrush->add_ImageOpened(
Callback<IRoutedEventHandler>([ellipseAsUIElement, ellipseAsFrameworkElement, imageSourceAsBitmap, strongParentElement, isVisible](
Callback<IRoutedEventHandler>([weakEllipse, imageSourceAsBitmap, weakParent, isVisible](
Copy link
Member

Choose a reason for hiding this comment

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

Callback [](start = 20, length = 8)

Hmm... perhaps we should audit all of our uses of Callback lambdas? e.g. ArrangeButtonContent() in ActionHelpers.cpp looks like it might have the same issue...

Copy link
Member

Choose a reason for hiding this comment

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

(of course, you may have already done this)


In reply to: 331661884 [](ancestors = 331661884)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. Logged #3511 to follow up.


In reply to: 331663216 [](ancestors = 331663216,331661884)

IInspectable* /*sender*/, IRoutedEventArgs * /*args*/) -> HRESULT {
if (isVisible)
{
RETURN_IF_FAILED(ellipseAsUIElement->put_Visibility(Visibility::Visibility_Visible));
RETURN_IF_FAILED(XamlHelpers::SetAutoImageSize(ellipseAsFrameworkElement.Get(),
strongParentElement.Get(),
imageSourceAsBitmap.Get(),
isVisible));
ComPtr<IFrameworkElement> lambdaEllipseAsFrameworkElement;
RETURN_IF_FAILED(weakEllipse.As(&lambdaEllipseAsFrameworkElement));

ComPtr<IInspectable> lambdaParentElement;
RETURN_IF_FAILED(weakParent.As(&lambdaParentElement));

if (lambdaEllipseAsFrameworkElement && lambdaParentElement)
{
RETURN_IF_FAILED(XamlHelpers::SetAutoImageSize(lambdaEllipseAsFrameworkElement.Get(),
lambdaParentElement.Get(),
imageSourceAsBitmap.Get(),
isVisible));
}
}
return S_OK;
}).Get(),
Expand Down Expand Up @@ -743,16 +759,33 @@ namespace AdaptiveNamespace
THROW_IF_FAILED(imageAsUIElement->put_Visibility(Visibility::Visibility_Collapsed));

// Handle ImageOpened event so we can check the imageSource's size to determine if it fits in its parent
ComPtr<IInspectable> strongParentElement(parentElement);
ComPtr<IInspectable> localParentElement(parentElement);

// Take weak references to the image and parent to avoid circular references between this lambda and
// its parents (Parent->Image->Lambda->(Parent and Image))
WeakRef weakParent;
THROW_IF_FAILED(localParentElement.AsWeak(&weakParent));

WeakRef weakImage;
THROW_IF_FAILED(imageAsFrameworkElement.AsWeak(&weakImage));
EventRegistrationToken eventToken;
THROW_IF_FAILED(xamlImage->add_ImageOpened(
Callback<IRoutedEventHandler>([imageAsFrameworkElement, strongParentElement, imageSourceAsBitmap, isVisible](
IInspectable* /*sender*/, IRoutedEventArgs *
/*args*/) -> HRESULT {
return XamlHelpers::SetAutoImageSize(imageAsFrameworkElement.Get(),
strongParentElement.Get(),
imageSourceAsBitmap.Get(),
isVisible);
Callback<IRoutedEventHandler>([weakImage, weakParent, imageSourceAsBitmap, isVisible](IInspectable* /*sender*/, IRoutedEventArgs *
/*args*/) -> HRESULT {
ComPtr<IFrameworkElement> lambdaImageAsFrameworkElement;
RETURN_IF_FAILED(weakImage.As(&lambdaImageAsFrameworkElement));

ComPtr<IInspectable> lambdaParentElement;
RETURN_IF_FAILED(weakParent.As(&lambdaParentElement));

if (lambdaImageAsFrameworkElement && lambdaParentElement)
{
RETURN_IF_FAILED(XamlHelpers::SetAutoImageSize(lambdaImageAsFrameworkElement.Get(),
lambdaParentElement.Get(),
imageSourceAsBitmap.Get(),
isVisible));
}
return S_OK;
}).Get(),
&eventToken));
}
Expand Down