Skip to content

FrameworkElementExtensions.Ancestor causes an XAML memory leak #107

@sylveon

Description

@sylveon

Describe the bug

Setting the AncestorType property on an object will store the parent of the object as an attached property, causing a reference loop (the parent holds a reference to the child, the child holds a reference to the parent).

  • Is this bug a regression in the toolkit? If so, what toolkit version did you last see it work:

Steps to Reproduce

  • Can this be reproduced in the Sample App? Technically yes, although the memory usage impact is hard to notice because there is a lot else going on in the sample app.

Steps to reproduce the behavior:

  1. Use AncestorType in some XAML content
  2. Load the XAML content (eg navigate to it in a frame)
  3. Unload the XAML content (eg navigate away from it, remove it from the visual tree)
  4. Notice that the XAML elements are never freed correctly, keep taking memory, and weak references will still resolve.

Expected behavior

The elements should be freed.

Screenshots

No screenshots needed.

Environment

NuGet Package(s): N/A

Package Version(s): N/A

Windows 10 Build Number:
- [ ] Fall Creators Update (16299)
- [ ] April 2018 Update (17134)
- [ ] October 2018 Update (17763)
- [ ] May 2019 Update (18362)
- [x] May 2020 Update (19041)
- [ ] Insider Build (build number: )

App min and target version:
- [ ] Fall Creators Update (16299)
- [ ] April 2018 Update (17134)
- [ ] October 2018 Update (17763)
- [ ] May 2019 Update (18362)
- [x] May 2020 Update (19041)
- [x] Insider Build (22000)

Device form factor:
- [x] Desktop
- [ ] Xbox
- [ ] Surface Hub
- [ ] IoT

Visual Studio
- [ ] 2017 (version: )
- [ ] 2019 (version: )
- [ ] 2019 Preview (version: )
- [x] 2022 Preview (version 17.0 preview 2.1)

Additional context

Found while doing a C++ port, so bug happens on latest commit. The solution to this issue would be to set the Ancestor attached property to null in the Unloaded event handler.

Metadata

Metadata

Labels

help wantedExtra attention is needed

Type

No type

Projects

Status

✅ Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions