-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Memory Leak in Style with Trigger - fix (#22503)
- Loading branch information
Showing
3 changed files
with
73 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,4 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui22036"/> |
58 changes: 58 additions & 0 deletions
58
src/Controls/tests/Xaml.UnitTests/Issues/Maui22036.xaml.cs
This file contains 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,58 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.Maui.ApplicationModel; | ||
using Microsoft.Maui.Controls.Core.UnitTests; | ||
using Microsoft.Maui.Dispatching; | ||
using Microsoft.Maui.UnitTests; | ||
using NUnit.Framework; | ||
|
||
namespace Microsoft.Maui.Controls.Xaml.UnitTests; | ||
|
||
public partial class Maui22036 | ||
{ | ||
public Maui22036() | ||
{ | ||
InitializeComponent(); | ||
} | ||
|
||
public Maui22036(bool useCompiledXaml) | ||
{ | ||
//this stub will be replaced at compile time | ||
} | ||
|
||
[TestFixture] | ||
class Test | ||
{ | ||
[SetUp] | ||
public void Setup() | ||
{ | ||
Application.SetCurrentApplication(new MockApplication()); | ||
DispatcherProvider.SetCurrent(new DispatcherProviderStub()); | ||
} | ||
|
||
[TearDown] public void TearDown() => AppInfo.SetCurrent(null); | ||
|
||
[Test] | ||
public async Task StyleWithTriggerLeak([Values(false, true)] bool useCompiledXaml) | ||
{ | ||
var style = new Style(typeof(ContentPage)); | ||
var trigger = new EventTrigger { Event = nameof(Appearing) }; | ||
trigger.Actions.Add(new EmptyTriggerAction()); | ||
style.Triggers.Add(trigger); | ||
|
||
Application.Current.Resources.Add(style); | ||
|
||
var pagewr = new WeakReference(new Maui22036(useCompiledXaml)); | ||
await Task.Delay(10); | ||
GC.Collect(); | ||
Assert.IsNull(pagewr.Target, "Page leaked"); | ||
} | ||
} | ||
|
||
class EmptyTriggerAction : TriggerAction<ContentPage> | ||
{ | ||
protected override void Invoke(ContentPage sender) | ||
{ | ||
} | ||
} | ||
} |