From adcaf6a317038033391f3d6a22e4b702118eea1b Mon Sep 17 00:00:00 2001 From: flexxxxer Date: Thu, 3 Aug 2023 22:38:52 +0300 Subject: [PATCH 1/6] fix: try 1 --- src/Avalonia.Controls/TopLevel.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Avalonia.Controls/TopLevel.cs b/src/Avalonia.Controls/TopLevel.cs index 04a5a0e6aa..66e402d642 100644 --- a/src/Avalonia.Controls/TopLevel.cs +++ b/src/Avalonia.Controls/TopLevel.cs @@ -591,6 +591,7 @@ namespace Avalonia.Controls Renderer.SceneInvalidated -= SceneInvalidated; // We need to wait for the renderer to complete any in-flight operations Renderer.Dispose(); + StopRendering(); Debug.Assert(PlatformImpl != null); // The PlatformImpl is completely invalid at this point From b0f6d17c558fd7ece0c3d56d090433087de66871 Mon Sep 17 00:00:00 2001 From: flexxxxer Date: Sat, 5 Aug 2023 00:31:33 +0300 Subject: [PATCH 2/6] unit tests added --- .../WindowDataContextTests.cs | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 tests/Avalonia.LeakTests/WindowDataContextTests.cs diff --git a/tests/Avalonia.LeakTests/WindowDataContextTests.cs b/tests/Avalonia.LeakTests/WindowDataContextTests.cs new file mode 100644 index 0000000000..239b090515 --- /dev/null +++ b/tests/Avalonia.LeakTests/WindowDataContextTests.cs @@ -0,0 +1,70 @@ +using System; +using Avalonia.Controls; +using Avalonia.Controls.ApplicationLifetimes; +using Avalonia.Reactive; +using Avalonia.Threading; +using Avalonia.UnitTests; +using JetBrains.dotMemoryUnit; +using Xunit; +using Xunit.Abstractions; + +namespace Avalonia.LeakTests; + +internal class ViewModelForDisposingTest +{ + ~ViewModelForDisposingTest() { ; } +} + +[DotMemoryUnit(FailIfRunWithoutSupport = false)] +public class WindowDataContextTests +{ + public WindowDataContextTests(ITestOutputHelper atr) + { + DotMemoryUnitTestOutput.SetOutputMethod(atr.WriteLine); + } + + [Fact] + public void Window_DataContext_Disposed_After_Window_Close_With_Lifetime() + { + static IDisposable Run() + { + var unitTestApp = UnitTestApplication.Start(TestServices.StyledWindow); + var lifetime = new ClassicDesktopStyleApplicationLifetime(); + lifetime.ShutdownMode = ShutdownMode.OnExplicitShutdown; + var window = new Window { DataContext = new ViewModelForDisposingTest() }; + window.Show(); + window.Close(); + + return Disposable.Create(lifetime, lt => lt.Shutdown()) + .DisposeWith(new CompositeDisposable(lifetime, unitTestApp)); + } + + using var _ = Run(); + // Process all Loaded events to free control reference(s) + Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded); + GC.Collect(); + + dotMemory.Check(m => Assert.Equal(0, + m.GetObjects(o => o.Type.Is()).ObjectsCount)); + } + + [Fact] + public void Window_DataContext_Disposed_After_Window_Close_Without_Lifetime() + { + static void Run() + { + using var _ = UnitTestApplication.Start(TestServices.StyledWindow); + var window = new Window { DataContext = new ViewModelForDisposingTest() }; + window.Show(); + window.Close(); + } + + Run(); + // Process all Loaded events to free control reference(s) + Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded); + GC.Collect(); + + dotMemory.Check(m => Assert.Equal(0, + m.GetObjects(o => o.Type.Is()).ObjectsCount)); + } +} From c418442d1345ee8ea5ed113ae9453bd740088c08 Mon Sep 17 00:00:00 2001 From: flexxxxer Date: Sat, 5 Aug 2023 17:17:57 +0300 Subject: [PATCH 3/6] Revert "unit tests added" This reverts commit b0f6d17c558fd7ece0c3d56d090433087de66871. --- .../WindowDataContextTests.cs | 70 ------------------- 1 file changed, 70 deletions(-) delete mode 100644 tests/Avalonia.LeakTests/WindowDataContextTests.cs diff --git a/tests/Avalonia.LeakTests/WindowDataContextTests.cs b/tests/Avalonia.LeakTests/WindowDataContextTests.cs deleted file mode 100644 index 239b090515..0000000000 --- a/tests/Avalonia.LeakTests/WindowDataContextTests.cs +++ /dev/null @@ -1,70 +0,0 @@ -using System; -using Avalonia.Controls; -using Avalonia.Controls.ApplicationLifetimes; -using Avalonia.Reactive; -using Avalonia.Threading; -using Avalonia.UnitTests; -using JetBrains.dotMemoryUnit; -using Xunit; -using Xunit.Abstractions; - -namespace Avalonia.LeakTests; - -internal class ViewModelForDisposingTest -{ - ~ViewModelForDisposingTest() { ; } -} - -[DotMemoryUnit(FailIfRunWithoutSupport = false)] -public class WindowDataContextTests -{ - public WindowDataContextTests(ITestOutputHelper atr) - { - DotMemoryUnitTestOutput.SetOutputMethod(atr.WriteLine); - } - - [Fact] - public void Window_DataContext_Disposed_After_Window_Close_With_Lifetime() - { - static IDisposable Run() - { - var unitTestApp = UnitTestApplication.Start(TestServices.StyledWindow); - var lifetime = new ClassicDesktopStyleApplicationLifetime(); - lifetime.ShutdownMode = ShutdownMode.OnExplicitShutdown; - var window = new Window { DataContext = new ViewModelForDisposingTest() }; - window.Show(); - window.Close(); - - return Disposable.Create(lifetime, lt => lt.Shutdown()) - .DisposeWith(new CompositeDisposable(lifetime, unitTestApp)); - } - - using var _ = Run(); - // Process all Loaded events to free control reference(s) - Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded); - GC.Collect(); - - dotMemory.Check(m => Assert.Equal(0, - m.GetObjects(o => o.Type.Is()).ObjectsCount)); - } - - [Fact] - public void Window_DataContext_Disposed_After_Window_Close_Without_Lifetime() - { - static void Run() - { - using var _ = UnitTestApplication.Start(TestServices.StyledWindow); - var window = new Window { DataContext = new ViewModelForDisposingTest() }; - window.Show(); - window.Close(); - } - - Run(); - // Process all Loaded events to free control reference(s) - Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded); - GC.Collect(); - - dotMemory.Check(m => Assert.Equal(0, - m.GetObjects(o => o.Type.Is()).ObjectsCount)); - } -} From ca2ca4ee9fae2e20155da6bd3148dc3e999fe57c Mon Sep 17 00:00:00 2001 From: flexxxxer Date: Sat, 5 Aug 2023 17:20:34 +0300 Subject: [PATCH 4/6] Revert "fix: try 1" This reverts commit adcaf6a317038033391f3d6a22e4b702118eea1b. --- src/Avalonia.Controls/TopLevel.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Avalonia.Controls/TopLevel.cs b/src/Avalonia.Controls/TopLevel.cs index 66e402d642..04a5a0e6aa 100644 --- a/src/Avalonia.Controls/TopLevel.cs +++ b/src/Avalonia.Controls/TopLevel.cs @@ -591,7 +591,6 @@ namespace Avalonia.Controls Renderer.SceneInvalidated -= SceneInvalidated; // We need to wait for the renderer to complete any in-flight operations Renderer.Dispose(); - StopRendering(); Debug.Assert(PlatformImpl != null); // The PlatformImpl is completely invalid at this point From 5b182890f3c3dece03e81c79ae1bdf889e85961c Mon Sep 17 00:00:00 2001 From: flexxxxer Date: Sat, 5 Aug 2023 17:27:31 +0300 Subject: [PATCH 5/6] Added unit tests for non-disposable DataContext issue (#12123) --- tests/Avalonia.LeakTests/DataContextTests.cs | 70 ++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 tests/Avalonia.LeakTests/DataContextTests.cs diff --git a/tests/Avalonia.LeakTests/DataContextTests.cs b/tests/Avalonia.LeakTests/DataContextTests.cs new file mode 100644 index 0000000000..d16f8c1f57 --- /dev/null +++ b/tests/Avalonia.LeakTests/DataContextTests.cs @@ -0,0 +1,70 @@ +using System; +using Avalonia.Controls; +using Avalonia.Controls.ApplicationLifetimes; +using Avalonia.Reactive; +using Avalonia.Threading; +using Avalonia.UnitTests; +using JetBrains.dotMemoryUnit; +using Xunit; +using Xunit.Abstractions; + +namespace Avalonia.LeakTests; + +internal class ViewModelForDisposingTest +{ + ~ViewModelForDisposingTest() { ; } +} + +[DotMemoryUnit(FailIfRunWithoutSupport = false)] +public class DataContextTests +{ + public DataContextTests(ITestOutputHelper atr) + { + DotMemoryUnitTestOutput.SetOutputMethod(atr.WriteLine); + } + + [Fact] + public void Window_DataContext_Disposed_After_Window_Close_With_Lifetime() + { + static IDisposable Run() + { + var unitTestApp = UnitTestApplication.Start(TestServices.StyledWindow); + var lifetime = new ClassicDesktopStyleApplicationLifetime(); + lifetime.ShutdownMode = ShutdownMode.OnExplicitShutdown; + var window = new Window { DataContext = new ViewModelForDisposingTest() }; + window.Show(); + window.Close(); + + return Disposable.Create(lifetime, lt => lt.Shutdown()) + .DisposeWith(new CompositeDisposable(lifetime, unitTestApp)); + } + + using var _ = Run(); + // Process all Loaded events to free control reference(s) + Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded); + GC.Collect(); + + dotMemory.Check(m => Assert.Equal(0, + m.GetObjects(o => o.Type.Is()).ObjectsCount)); + } + + [Fact] + public void Window_DataContext_Disposed_After_Window_Close_Without_Lifetime() + { + static void Run() + { + using var _ = UnitTestApplication.Start(TestServices.StyledWindow); + var window = new Window { DataContext = new ViewModelForDisposingTest() }; + window.Show(); + window.Close(); + } + + Run(); + // Process all Loaded events to free control reference(s) + Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded); + GC.Collect(); + + dotMemory.Check(m => Assert.Equal(0, + m.GetObjects(o => o.Type.Is()).ObjectsCount)); + } +} From 628455ec3d3291083d76216de9d9b7934d1fcfcd Mon Sep 17 00:00:00 2001 From: flexxxxer Date: Sat, 5 Aug 2023 17:29:17 +0300 Subject: [PATCH 6/6] To Avalonia.Controls.TopLevel.HandleClosed method body was added Avalonia.Controls.TopLevel.StopRendering method call --- src/Avalonia.Controls/TopLevel.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Avalonia.Controls/TopLevel.cs b/src/Avalonia.Controls/TopLevel.cs index 04a5a0e6aa..66e402d642 100644 --- a/src/Avalonia.Controls/TopLevel.cs +++ b/src/Avalonia.Controls/TopLevel.cs @@ -591,6 +591,7 @@ namespace Avalonia.Controls Renderer.SceneInvalidated -= SceneInvalidated; // We need to wait for the renderer to complete any in-flight operations Renderer.Dispose(); + StopRendering(); Debug.Assert(PlatformImpl != null); // The PlatformImpl is completely invalid at this point