Skip to content

Commit e6c62d6

Browse files
authored
Fix GC Race condition with tests (#27652)
1 parent 2075725 commit e6c62d6

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

src/Controls/tests/Core.UnitTests/TestClasses/TestWindow.cs

+5-6
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Runtime.CompilerServices;
5-
using System.Text;
6-
using System.Threading.Tasks;
75

86
namespace Microsoft.Maui.Controls.Core.UnitTests
97
{
108
public class TestWindow : Window
119
{
10+
// Because the relationship from Window => Application is a weakreference
11+
// we need to retain a reference to the Application so it doesn't get GC'd
12+
TestApp _app;
1213
public TestWindow()
1314
{
1415

@@ -24,12 +25,10 @@ protected override void OnPropertyChanged([CallerMemberName] string propertyName
2425
if (propertyName == PageProperty.PropertyName &&
2526
Parent == null)
2627
{
27-
var app = new TestApp(this);
28-
_ = (app as IApplication).CreateWindow(null);
28+
_app = new TestApp(this);
29+
_ = (_app as IApplication).CreateWindow(null);
2930
}
3031
}
31-
32-
3332
}
3433

3534
public static class TestWindowExtensions

0 commit comments

Comments
 (0)