We are currently playing a 4K h.265 video in an UWP app as background using MediaPlayerElement. We have set the MediaPlayer to infinite loop using IsLoopingEnabled = true. The problem is, that the memory usage is increasing every time the video loops. If we disable looping, the memory leak does not occure. We tried looping the video manually by resetting the position to zero when the video finished, but still it leaks memory. We also tried to call System.GC.collect() but that also did nothing.
Is this a UWP bug or are we missing something?
Edit:
Here is the code we are using:
MainPage.xaml
<Page
x:Class="MyProject.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="using:MyApp.Controls"
xmlns:xaml="using:Microsoft.Graphics.Canvas.UI.Xaml"
mc:Ignorable="d"
Background="Black" Loaded="MainPage_OnLoaded" Unloaded="MainPage_OnUnloaded">
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="{StaticResource AppBrushNewBlue1}">
<MediaPlayerElement Name="bgMovie" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AreTransportControlsEnabled="False" ></MediaPlayerElement>
<Canvas Name="mainCanvas" ManipulationMode="None" Background="Transparent">
</Canvas>
</Grid>
MainPage.xaml.cs
private MediaSource ms;
private async void MainPage_OnLoaded(object sender, RoutedEventArgs e)
{
ms = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/bg_animation_V2.mp4"));
bgMovie.Source = ms;
bgMovie.MediaPlayer.IsLoopingEnabled = true;
bgMovie.MediaPlayer.Play();
[...]
}
We are not doing anything more with the MediaSource or the mediaPlayer itself. When we disable IsLoopingEnabled no memory leak occurs anymore.
As stated here https://stackoverflow.com/a/54947557/1018232 this seems to be a bug in Windows 10. Even the builtin video player "Movies and TV" has this issue. It seems this only happens on h.265 codec. Maybe it is a decoder driver bug or something like this.
There is always a confusing amount of different Media Player elements in WPF. You are using teh MediaPlayerElement. And it's documentation explicitly mentions that it is still in a open Beta-ish phase:
This control is currently available as a developer preview for Windows 10, version 1903, and later. Although we encourage you to try out this control in your own prototype code now, we do not recommend that you use it in production code at this time. For more information, see the XAML Islands feature roadmap. If you have feedback about this control, create a new issue in the Microsoft.Toolkit.Win32 repo and leave your comments there. If you prefer to submit your feedback privately, you can send it to XamlIslandsFeedback#microsoft.com.
So it is entirely possible you just found a bug.
As far as I can tell, the approach to playing Media before the MPE is the Storyboard and the MediaPlayer/MediaElement combination. As far as I can tell, MediaPlayer has no repeat mode. So a event will have to do. Someone with deeper WPF/UWP knowledge will have to tell you wich one is the right one for the time being.
Related
I am working on webview control in C# UWP app. Everything was working. All at a sudden webview stop showing the webpage on the window. I tried creating one more app and rewrite the code once more. But it's not working now.
Below is the code.
<Page
x:Class="hello.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:hello"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid >
<WebView x:Name="webView" Source="http://www.www.bing.com" />
</Grid>
</Page>
ERROR :
I tried using other URLs as well. Tried webView.Navigate(new Uri("http://www.bing.com")); in the cs file as well , but webview is not at all appearing in the window. Searched a lot but did not get any solution.
All break points are getting hit properly and it's not throwing any error while running. Cleaned the debug folder manually and rebuild the app again .. but no luck.
Can someone help me to fix the issue ?
C# UWP WebView Not appearing on UWP mainpage
I checked your code, I found you have passed wrong uri http://www.www.bing.com, and if you want to make the webview fill the Grid you need set VerticalAlignment="Stretch" HorizontalAlignment="Stretch", I edited your code, and it works, please refer the following.
<Grid>
<WebView
x:Name="webView"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Source="http://www.bing.com"
/>
</Grid>
You could call webView.Navigate(new Uri("http://www.bing.com")) method in the page Loaded event, and remove above Source property in xaml code.
private void Page_Loaded(object sender, RoutedEventArgs e)
{
webView.Navigate(new Uri("http://www.bing.com"));
}
Please note your WebView will access internet, so you need check internet capability.
Thanks very much , Internet was the root cause , adding that solved the problem.
I experienced what appears to be a very obvious memory leak in an application that would open a settings window containing a TabControl with a number of TabItems. Initially believing that one of the user controls shown must be the culprit I commented out a bunch of things, finally getting out JetBrains dotMemory and making a demo-program.
The issue (I think)
When a Window contains a TabControl with at least one TabItem, when the window is closed, the Window-object still exists. If there are no TabItems in the TabControl, the Window-object is destroyed immediately (as expected).
Retention
According to dotMemory the 'Retention' is from WindowAutomationPeer(._owner), from TabControlAutomationPeer(._parent), from TabItemAutomationPeer(._parent), from ElementProxy(._peer) and then it says "RefCounted handle" at the bottom.
Reproduction
Create a new C# WPF application (Target framework: .NET Framework 4.7.2), named "TabsInWindows"
Add a button to MainWindow:
<Window x:Class="TabsInWindows.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="300">
<Grid>
<Button Content="Open tab window" Click="Button_Click"/>
</Grid>
</Window>
Create a new Window, "TabsWindow" with a TabControl and a TabItem:
<Window x:Class="TabsInWindows.TabsWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="TabsWindow" Height="200" Width="300">
<Grid>
<TabControl x:Name="Subject">
<TabItem></TabItem>
</TabControl>
</Grid>
</Window>
Make the button in MainWindow open a new TabWindow
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//TabsWindow.Open(this);
TabsWindow w = new TabsWindow();
w.Show();
}
}
Start the application. Every time you press the button a new window is created, but the TabItem (?) and therefore the TabWindow remains when the window is closed. (Repeat any number of times)
How to fix?
My issue is that in my actual application all contents of all tabs appears to be retained in memory, causing a significant memory leak.
I have tried, in the demo application, to do a number of things to avoid the hanging objects; Setting content of Grid to null. Clearing the Items in TabControl (Subject). Clearing the Children of the Grid.
None of it has worked.
I cannot work out what the 'AutomationPeer'-objects are or what the ElementProxy is created by and why it won't die.
If anyone can tell me how to get around this issue, or can shed some light on what ElementProxy is and why it is hanging around, it would be most helpful.
While writing this, I did keep dotMemory running with the test app and a while after having done anything last, the objects did appear to have been removed....
Which then raises the question: How long can I expect an object to be visible in memory, with references, before it is removed?
In an actual project
I then tried something similar in an actual project, ensuring that non of our own controls were directly linked to SettingsWindow (I'm not ruling out that I have a memory/reference issue in one of our controls, so any control listed directly in "Key Retention Paths" have been commented out).
I am left with "3 unique branches", one being an 'EffectiveValueEntry[40]' from out own extension of a ListBox, the other two are 'EffectiveValueEntry' ([19] and [22] respectively), both from a TextBlock, from TextBlockAutomationPeer[4], List, ListBoxItemAutomationPeer, ElementProxy.
After about ten minutes of doing nothing, the SettingsWindow was still there, but the "Key Retention Paths" has changed, and the "20 unique branches" are all EffectiveValueEntry ([32] on the first, [42] on the rest), TextBox, TextEditor, but now "F-Reachable Queue" is in the bottom of the list.
After about ten minutes more, the SettingsWindow was finally gone.
I then opened the settings window a few times again, and a minute after closing the last, only the 'TextBox'-references where left and a forced Garbage Collection later (using the button in dotMemory), the object references are gone.
What to believe?
So apparently, if I wait long enough 'magic' will happen - but this is a computer - not a magic-box!
Can anyone enlighten me on why some objects will appear in memory longer, but eventually be removed? How long should I expect such objects to lay about?
I would also like a way to prevent these 'ghost' objects from the TabItems, there should be no reason for them to take up memory if they will eventually be removed anyways...
You see, I discovered this while doing performance testing of some UI components in the SettingsWindow, and repeated tests took longer and longer as more memory was used, so simply waiting for the references to go away is not a very good option.
And if you are unable to help; thank you for taking the time to read my wall of text...
Set the owner of the TabWindow's
private void Button_Click(object sender, RoutedEventArgs e)
{
TabsWindow w = new TabsWindow();
w.Owner = this;
w.Show();
}
So I've googled all day trying to find an answer and have come up short. I've found stuff close and maybe even found a solution and just didn't realized it but I cant seem to get the Minimize, Maximize / Restore, and Close buttons to show up (be visible) on my windows 10 machine. No one but myself and another developer who just got new laptops have this issue. I've tried changing my windows theme around and I did get them to show up with I turned some high contrast setting on but no luck otherwise. Despite not being visible they are there and functional because I can click in the area and see the window min, max, restore, close.
We are using .Net 4.0 and a RibbonWindow with a custom theme (BureauBlue.xaml). I'd like to believe it may have something to do with that but I don't know anything about it or where to start unfortunately.
<r:RibbonWindow x:Class="Ztools.Main" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:props="clr-namespace:Ztools.Properties"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:r="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
IsTabStop="False" Icon="/Ztools;component/ztools32.ico" Height="830" Width="1200" WindowStartupLocation="CenterScreen" WindowState="Normal"
Loaded="RibbonWindow_Loaded" Closing="RibbonWindow_Closing"
xmlns:my="clr-namespace:System;assembly=mscorlib" Title="Ztools 2.0" Name="mainRibbon" FontSize="14" SizeChanged="mainRibbon_SizeChanged" LocationChanged="mainRibbon_LocationChanged"
StateChanged="mainRibbon_StateChanged" Deactivated="mainRibbon_Deactivated" KeyUp="mainRibbon_KeyUp" Background="{x:Null}">
<r:RibbonWindow.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Ztools;component/themes/bureaublue.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</r:RibbonWindow.Resources>
</r:RibbonWindow>
Another thing I noticed but haven't verified by pulling out my old laptop yet is I'm pretty sure the nice looking buttons showed up at design time as well and didn't look like this.
Bonus question/issue is sometimes when our computers go to sleep/hibernate go from docking station to no station (not sure which one or both) the theme bar will also randomly black out like this. If I could look at fixing this at the same time that would be great.
I did change it to a normal Window and was able to see all the buttons correctly but I guess I'd rather not go that route and know what the issue is and solve it.
Any thoughts ideas or suggestions are greatly appreciated.
Edit: So I don't think it has anything to do with the theme? I commented out everything having to do with the theme and they still don't show up... Not sure why I didn't take that simple step a long time ago.
So going to post this again that way anyone with the same or similar issue can at least have an option to fixing their issue... since for some reason it was deleted despite containing valuable information as an alternative solution to the problem.
For now I changed it from a RibbonWindow to a Window an gave my Ribbon a margin of 0,-22,0,0 so things line up and look decent as suggested by a number of other SO posts. The buttons show up now, but aren't the RibbonWindow style so is what it is.
<Window x:Class="Ztools.Main" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:props="clr-namespace:Ztools.Properties"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:r="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
IsTabStop="False" Icon="/Ztools;component/ztools32.ico" Height="830" Width="1200" WindowStartupLocation="CenterScreen" WindowState="Normal"
Loaded="RibbonWindow_Loaded" Closing="RibbonWindow_Closing"
xmlns:my="clr-namespace:System;assembly=mscorlib" Title="Ztools 2.0" Name="mainRibbon" FontSize="14" SizeChanged="mainRibbon_SizeChanged" LocationChanged="mainRibbon_LocationChanged" StateChanged="mainRibbon_StateChanged" Deactivated="mainRibbon_Deactivated" KeyUp="mainRibbon_KeyUp">
<r:Ribbon Title="Ztools 2.0 (Scale Configuration Editor)" IsTabStop="False" Background="#FFE5E5E5" FontSize="12" FontFamily="Arial" Margin="0,-22,0,0">
</r:Ribbon>
</Window>
The ribbonwindow in version 5 has set WindowStyle="none" by default.
Maybe the WindowStyle is just set to None?
Try set it to "SingleBorderWindow" its original default in the base class.
I not only had the same issue but was able to replicate it in a new project and fix it through this solution although for you it will require moving to a more recent .net version.
The problem seems to stem from using RibbonControlsLibrary. It's is an outdated version of the ribbon controls. As of .net 4.5 Ribbon is native to the framework and by removing the reference you'll be able to use the included RibbonWindow.
Move to a .net version 4.5+ and remove this reference
xmlns:r="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
and use
<RibbonWindow>
instead of
<r:RibbonWindow>
If I create a new WPF application with a simple empty window like the code shown below, I find that all applications which are covered by the WPF app lost touch or stylus reaction. This can only be reproduced when Windows 10 is upgraded to 1803 (10.0.17134.0).
<Window x:Class="TheWPFCoveringWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowStyle="None" WindowState="Maximized"
AllowsTransparency="True" Background="Transparent"
Topmost="True">
<Button Content="Test" Width="200" Height="100" />
</Window>
I wrote another WPF application to find out what happened. So I add a StylusDown event to the Window like the code shown below:
// This code is in another WPF application.
private void OnStylusDown(object sender, StylusDownEventArgs e)
{
// Set a breakpoint here.
}
But the breakpoint never reached until I closed the transparent WPF window which is on top.
I pushed the very simple code to GitHub: dotnet-campus/TouchIssueOnWindows10.0.17134. Cloning it might help a little.
Why does this happen and how to solve it? Any reply is appreciated.
Updated
Microsoft has fixed this issue in .NET Framework August 2018 Preview of Quality Rollup.
August 30, 2018—KB4346783 (OS Build 17134.254)
Addresses an issue where touch and mouse events were handled differently in Windows Presentation Foundation (WPF) applications that have a transparent overlay window.
Original
After a whole week's debugging, I finally find out the solution.
Just add a ResizeMode="NoResize" property for the Window as the code shown below:
<Window x:Class="TheWPFCoveringWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowStyle="None" WindowState="Maximized"
AllowsTransparency="True" ResizeMode="NoResize"
Background="Transparent" Topmost="True">
<Button Content="Test" Width="200" Height="100" />
</Window>
#lindexi has posted this issue and this solution into his post. If you want more information about this issue, read win10 17025 touch bug - lindexi for more details. (This post is written in multiple languages, so you'll miss nothing even if you ignore the unknown characters.)
Actually, I still can't figure out why this property helps.
Could anyone explain the reason for this issue?
I am working on a project that makes use of the WPF WebBrowser control (System.Windows.Controls.WebBrowser). The web browser element of the program is one of many activities the user can engage in, and is opened in a separate window. After the user navigates away from the browser, the window is closed, and a new window is created each time the user returns to the browser. We were noticing a significant memory leak / performance downgrade in our program (usage getting up to ~700mb from ~200 initial) upon continually using the browser. After failing to find any points of resource leaks within our own code, I decided to determine if the issue was with our own WebBrowser wrapper control, or the WPF control.
I created a new simple project consisting of only a MainWindow and a WebWindow. A button on the main window launches a browser directed at gmail (the site we noticed the biggest issue with of the few we examined). Upon closing this window, there is no freeing of resources (no reduction in VM size in Task Manager or Process Explorer) and the number of GDI objects the process has handles to does not decrease (the program starts with ~30, opening the browser takes it to ~140 and after closing the browser ~140 are still open). Opening another browser causes more handles, and more resources to be allocated. Furthermore, this problem is not fixed by specifically calling Dispose() on the WebBrowser control. The code is simple, and is as follows:
Main Window:
<Window x:Class="WebBrowserMemory.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<Button Click="Button_Click">Gmail</Button>
</StackPanel>
</Grid>
</Window>
Button_Click:
private void Button_Click(object sender, RoutedEventArgs e)
{
var win = new WebWindow();
win.Show();
win.Browser.Navigate("http://www.gmail.com");
}
Web Window:
<Window x:Class="WebBrowserMemory.WebWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WebWindow" Height="300" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<WebBrowser Grid.Row="0" x:Name="_browser" />
<Button Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="10" Padding="10" Click="Button_Click">Close</Button>
</Grid>
</Window>
Relevant Code:
public WebBrowser Browser {
get { return _browser; }
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Close();
}
protected override void OnClosed(EventArgs e)
{
_browser.Dispose();
base.OnClosed(e);
}
Has anyone else encountered this issue using the WPF WebBrowser control?
[UPDATE: Updated post to indicate Dispose() call as per itowlson's answer - even calling Dispose() on the web browser control does not free the resources]
Unlike most WPF controls, WebBrowser (because it inherits from HwndHost) is IDisposable and encapsulates unmanaged resources. The WPF Window, unlike the WinForms Form, does not automatically dispose its children (because native WPF controls do not encapsulate unmanaged resources and do not require disposal).
Add an OnClosed override to your window (or handle the Closed event), and call Dispose on the WebBrowser control.
I have not been able to completely solve the leak, however,I have noticed that navigating the browser to "about:blank" prior to disposal definitely helps reduce the amount of memory that hangs around.
We instead used WinForm WebBrowser control, which was created inside FormsHost in WPF, however both work pretty same from the UI point of view, but we have found that WebBrowser of WinForms has better functionality and better performance compared to one given in WPF.
You can manually dispose WebBrowser of WinForm control that will certainly dispose all of its children and free resources accordingly, however with my past experience, WinForm's WebBrowser does not release 100% of its resources after closing, but yes it is far better then WPF.