-
Notifications
You must be signed in to change notification settings - Fork 1.4k
🚧 Correct ancestor XAML memory leak #4236
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
Closed
XAML-Knight
wants to merge
26
commits into
CommunityToolkit:main
from
XAML-Knight:dev/AncestorTheUnloadedJr
Closed
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
357d2ba
Wire up Unload event
XAML-Knight d529d48
Added FrameworkElementExtension test
XAML-Knight b2851d3
add test file, wip framework extension
XAML-Knight 8c9890b
Apply WeakReference
XAML-Knight 2f9a266
Added missing header
XAML-Knight c5b2bb7
Place DP props on one line
XAML-Knight 55316d4
Remove blank line
XAML-Knight 762818c
Add test for grid unload
XAML-Knight cd475a3
Apply Await
XAML-Knight 27c302f
Aid the unload effort
XAML-Knight 1206930
Test added to proj
XAML-Knight 669c355
Merge remote-tracking branch 'XAML-Knight/dev/AncestorTheUnloaded' in…
XAML-Knight 45ce6ad
Wire up Unload event
XAML-Knight 547cc92
Added FrameworkElementExtension test
XAML-Knight 6b049eb
add test file, wip framework extension
XAML-Knight a529c02
Apply WeakReference
XAML-Knight f35da93
Added missing header
XAML-Knight 9b937b9
Place DP props on one line
XAML-Knight 46e6072
Remove blank line
XAML-Knight eccae8a
Add test for grid unload
XAML-Knight 82b2819
Apply Await
XAML-Knight f99184c
Aid the unload effort
XAML-Knight 48e323c
Merge remote-tracking branch 'XAML-Knight/dev/AncestorTheUnloaded' in…
XAML-Knight afb83c5
Ignore Unit Tests to get build to pass
XAML-Knight 70c5a8e
Set dummy variable, as check that runs after main body of test
XAML-Knight 8396bf9
Detach from loaded, and test improvements
XAML-Knight File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
UnitTests/UnitTests.UWP/Extensions/Test_FrameworkElementExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using Microsoft.Toolkit.Uwp; | ||
| using Microsoft.Toolkit.Uwp.UI; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using System.Threading.Tasks; | ||
| using Windows.UI.Xaml.Controls; | ||
| using Windows.UI.Xaml.Markup; | ||
|
|
||
| namespace UnitTests.Extensions | ||
| { | ||
| [TestClass] | ||
| public class Test_FrameworkElementExtensions : VisualUITestBase | ||
| { | ||
| [Ignore] | ||
| [TestCategory("FrameworkElementExtensions")] | ||
| [TestMethod] | ||
| public async Task Test_Ancestor_WeakReference() | ||
| { | ||
| // Need to sim loading the control, and not just load the XAML via XamlReader | ||
| await App.DispatcherQueue.EnqueueAsync(async () => | ||
| { | ||
| var treeRoot = XamlReader.Load( | ||
| @"<Page | ||
| xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" | ||
| xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" | ||
| xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"" | ||
| xmlns:ui=""using:Microsoft.Toolkit.Uwp.UI""> | ||
|
|
||
| <Grid x:Name=""OuterGrid"" Width=""200"" Background=""Khaki""> | ||
| <Button x:Name=""InnerButton"" ui:FrameworkElementExtensions.AncestorType=""Grid"" /> | ||
| </Grid> | ||
|
|
||
| </Page>") as Page; | ||
|
|
||
| Assert.IsNotNull(treeRoot, "Could not load XAML tree."); | ||
| await SetTestContentAsync(treeRoot); | ||
|
|
||
| // Need to simulate loading the control (and not just rely upon XamlReader.Load) | ||
| var frame = new Frame(); | ||
|
|
||
| await App.DispatcherQueue.EnqueueAsync(() => frame.Navigate(treeRoot.GetType())); | ||
|
|
||
| Assert.IsInstanceOfType(frame.Content, typeof(Page)); | ||
|
|
||
| var btn = treeRoot.FindChild("InnerButton") as Button; | ||
|
|
||
| Assert.IsNull(btn.Parent); | ||
|
|
||
| await App.DispatcherQueue.EnqueueAsync(() => btn.SetValue(FrameworkElementExtensions.AncestorProperty, new object())); | ||
|
|
||
| Assert.IsNull(btn.Parent); | ||
| }); | ||
| } | ||
|
|
||
| [Ignore] | ||
| [TestCategory("FrameworkElementExtensions")] | ||
| [TestMethod] | ||
| public async Task Test_Ancestor_WeakRef_UnloadGrid() | ||
| { | ||
| // Need to sim loading the control, and not just load the XAML via XamlReader | ||
| await App.DispatcherQueue.EnqueueAsync(async () => | ||
| { | ||
| var placeholder = new object(); | ||
|
|
||
| var treeRoot = XamlReader.Load( | ||
| @"<Page | ||
| xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" | ||
| xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" | ||
| xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"" | ||
| xmlns:ui=""using:Microsoft.Toolkit.Uwp.UI""> | ||
|
|
||
| <Grid x:Name=""OuterGrid"" Width=""200"" Background=""Khaki""> | ||
| <Button x:Name=""InnerButton"" ui:FrameworkElementExtensions.AncestorType=""Grid"" /> | ||
| </Grid> | ||
|
|
||
| </Page>") as Page; | ||
|
|
||
| Assert.IsNotNull(treeRoot, "Could not load XAML tree."); | ||
| await SetTestContentAsync(treeRoot); | ||
|
|
||
| // Need to simulate loading the control (and not just rely upon XamlReader.Load) | ||
| var frame = new Frame(); | ||
|
|
||
| await App.DispatcherQueue.EnqueueAsync(() => frame.Navigate(treeRoot.GetType())); | ||
|
|
||
| Assert.IsInstanceOfType(frame.Content, typeof(Page)); | ||
|
|
||
| var grid = treeRoot.FindChild("OuterGrid") as Grid; | ||
| var button = treeRoot.FindChild("InnerButton") as Button; | ||
|
|
||
| await App.DispatcherQueue.EnqueueAsync(() => button.SetValue(FrameworkElementExtensions.AncestorProperty, new object())); | ||
|
|
||
| treeRoot.Unloaded += (sender, e) => | ||
| { | ||
| Assert.AreEqual(grid, null); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We probably want to have a value set here that we check is set below after the await SetTestContent as well, otherwise if this doesn't fire then we never know, but we want to make sure it fires, eh? |
||
| }; | ||
|
|
||
| // Allow treeRoot to Unload, before test ends | ||
| await SetTestContentAsync(null); | ||
|
|
||
| Assert.IsNotNull(placeholder); | ||
| }); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By changing the type here we break using it in XAML as intended. This can be demonstrated with the
TokenizingTextBoxas it uses this feature to grab theTokenSpacingproperty from its parent.