Skip to content

Conversation

@Ryken100
Copy link

@Ryken100 Ryken100 commented Jan 8, 2026

Fixes #816

This PR ensures that the SpriteVisual of each attached shadow is removed from the visual tree before disposing of the SpriteVisual or any other composition resources.

PR Type

What kind of change does this PR introduce?

Bugfix

What is the current behavior?

The attached shadow logic disposes of composition resources before the SpriteVisual is removed from the visual tree.

What is the new behavior?

AttachedShadowBase.OnElementContextUninitialized() and AttachedShadowElementContext.Uninitialize() were updated to remove the SpriteVisual from the visual tree before disposing of it and linked composition resources. This should prevent XAML from trying to access the SpriteVisual or its resources after they have been disposed.

PR Checklist

Please check if your PR fulfills the following requirements:

  • Created a feature/dev branch in your fork (vs. submitting directly from a commit on main)
  • Based off latest main branch of toolkit
  • Tested code with current supported SDKs
  • New component
    • Documentation has been added
    • Sample in sample app has been added
    • Analyzers are passing for documentation and samples
    • Icon has been created (if new sample) following the Thumbnail Style Guide and templates
  • Tests for the changes have been added (if applicable)
  • Header has been added to all new source files
  • Contains NO breaking changes

Other information

N/A

Remove SpriteVisual before disposing of resources
@Arlodotexe
Copy link
Member

Arlodotexe commented Jan 9, 2026

We should write a test to highlight the broken/fixed behavior.

@Ryken100
Copy link
Author

Ryken100 commented Jan 9, 2026

We should write a test to highlight the broken/fixed behavior.

Unfortunately I'm unable to repro the issue, so I'm not sure how to recreate broken/fixed states in a test.

For info, in our internal bug report someone from composition informed us that XAML is trying to access a hand-in visual (a visual added using ElementCompositionPreview.SetElementChildVisual) that has already been disposed, and showed the issue originated from a visual tree that uses an AttachedCardShadow's SpriteVisual as its hand-in visual.

The only spot I saw where we could be creating this issue is that we dispose of the SpriteVisual and other composition resources before removing the visual from the tree using ElementCompositionPreview.SetElementChildVisual(_, null).

@Arlodotexe
Copy link
Member

Thanks for the added details @Ryken100! I'll see if I can repro in a test using the info you've provided, we don't want to end up using prod to debug these things the long way around.

Copy link

@zubinqayam zubinqayam left a comment

Choose a reason for hiding this comment

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

Ok

Copy link

@zubinqayam zubinqayam left a comment

Choose a reason for hiding this comment

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

Ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The SpriteVisual should be removed from the visual tree before being disposed in AttachedShadowElementContext

4 participants