Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Samples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Note

The WinUIDesktopSample and WinUIDesktopSamplePackage contain repro scenarios for the memory leak we addressed around version 1.1.4.
These scenarios should be moved out of samples and into our unit test suite, so we can have automated verification.
Until we port ObjectLifetimeTests into our unit tests, these samples serve as a way to check we have not regressed any scenarios.
8 changes: 7 additions & 1 deletion src/Samples/WinUIDesktopSample/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Application.Resources>

<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
<!-- Other merged dictionaries here -->
</ResourceDictionary.MergedDictionaries>
<!-- Other app resources here -->
</ResourceDictionary>
</Application.Resources>
</Application>
63 changes: 26 additions & 37 deletions src/Samples/WinUIDesktopSample/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Windows.Web.Http;
using Microsoft.UI.Xaml;
using System;
using Windows.ApplicationModel;


namespace WinUIDesktopSample
{
Expand All @@ -16,44 +10,39 @@ namespace WinUIDesktopSample
/// </summary>
public partial class App : Application
{
private Window myWindow;

public App()
{
WinRT.ComWrappersSupport.RegisterProjectionAssembly(typeof(App).Assembly);
this.InitializeComponent();
this.Suspending += OnSuspending;
}

Window myWindow;
protected override void OnLaunched(LaunchActivatedEventArgs args)
public static void Navigate(Type pageType)
{
var value = DependencyProperty.UnsetValue;
var button = new Button
{
Content = "Click me to load MainPage",
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
button.Click += Button_Click;
var window = new Microsoft.UI.Xaml.Window
{
Content = button
};

window.Activate();

myWindow = window;
App app = (App)Current;
MainWindow mainWindow = (MainWindow)app.myWindow;
mainWindow.Frame.Navigate(pageType);
}

private void Button_Click(object sender, RoutedEventArgs e)
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
myWindow.Content = new MainPage();
myWindow = new MainWindow();
myWindow.Activate();
}
}

public static class Program
{
static void Main(string[] args)
{
WinRT.ComWrappersSupport.InitializeComWrappers();

Microsoft.UI.Xaml.Application.Start((e) => new App());
/// <summary>
/// Invoked when application execution is being suspended. Application state is saved
/// without knowing whether the application will be terminated or resumed with the contents
/// of memory still intact.
/// </summary>
/// <param name="sender">The source of the suspend request.</param>
/// <param name="e">Details about the suspend request.</param>
private void OnSuspending(object sender, SuspendingEventArgs e)
{
// Save application state and stop any background activity
}
}
}
16 changes: 16 additions & 0 deletions src/Samples/WinUIDesktopSample/FirstPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Page
x:Class="WinUIDesktopSample.FirstPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WinUIDesktopSample"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<StackPanel>
<Button Content="Navigate to SecondPage" Click="Button_Click" />
<TextBlock x:Name="Status" />
<ListView x:Name="TheListView" />
</StackPanel>
</Page>
53 changes: 53 additions & 0 deletions src/Samples/WinUIDesktopSample/FirstPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.Generic;
using System.Linq;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace WinUIDesktopSample
{
public class LeakedObject : Page
{
byte[] bytes = new byte[10_000_000];
};
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class FirstPage : Page
{
private static List<WeakReference> instances = new List<WeakReference>();

public FirstPage()
{
this.InitializeComponent();

this.TheListView.ItemsSource = GetItems();

GC.Collect(2, GCCollectionMode.Forced, true);
GC.WaitForPendingFinalizers();
var collected = instances.Count((WeakReference wr) => !wr.IsAlive);
var leaked = instances.Count((WeakReference wr) => wr.IsAlive);
System.Diagnostics.Debug.WriteLine("FirstPage instances: ");
System.Diagnostics.Debug.WriteLine(" collected: " + collected);
System.Diagnostics.Debug.WriteLine(" leaked: " + leaked);
this.Status.Text = "FirstPage instances: collected: " + collected + " leaked: " + leaked;
instances.Add(new WeakReference(this));
}

public IEnumerable<string> GetItems()
{
for (int i = 0; i < 1000; i++)
{
yield return "Item " + (i + 1);
}
}

private void Button_Click(object sender, RoutedEventArgs e)
{
App.Navigate(typeof(SecondPage));
}
}
}
28 changes: 24 additions & 4 deletions src/Samples/WinUIDesktopSample/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,28 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="WinUIDesktopSample"
mc:Ignorable="d"
Height="450" Width="800">
<Grid>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="Hello from WinUI Desktop!" x:Name="text"/>
</Grid>
Height="600" Width="800">


<StackPanel>
<Button x:Name="Button1" Content="Aggregation" Click="Button1Click" />
<Button x:Name="Button2" Content="AggregationAndProperty" Click="Button2Click" />
<Button x:Name="Button3" Content="AggregationAndEvent" Click="Button3Click" />
<Button x:Name="Button4" Content="Button" Click="Button4Click" />
<Button x:Name="Button5" Content="ButtonAndProperty" Click="Button5Click" />
<Button x:Name="Button6" Content="ButtonAndEvent" Click="Button6Click" />
<Button x:Name="Button7" Content="XamlMarkup-PageWithButton" Click="Button7Click" />
<Button x:Name="Button8" Content="XamlCode-PageWithButton" Click="Button8Click" />
<Button x:Name="Button9" Content="StrongButton" Click="Button9Click" />
<TextBlock x:Name="Build" Text="" />
<Button x:Name="WithoutCapture" Click="WithoutCapture_Click">Grid and handler without capture</Button>
<Button x:Name="WithCapture" Click="WithCapture_Click">Grid and handler with capture</Button>
<Button x:Name="DerivedWithoutCapture" Click="DerivedWithoutCapture_Click">Derived Grid and handler without capture</Button>
<Button x:Name="DerivedWithCapture" Click="DerivedWithCapture_Click">Derived Grid and handler with capture</Button>
<Button x:Name="Alloc" Click="Alloc_Click">1. Allocate various aggregates</Button>
<Button x:Name="Check" Click="Check_Click">2. Check for leaks of aggregates</Button>
<Button x:Name="NavigateButton" Click="Navigate_Click">Navigate to first page</Button>
<TextBlock x:Name="Status" Text="(click a button)" />
</StackPanel>

</Page>
Loading