Skip to content

Commit 8f57ed4

Browse files
authored
Fix: Fixed OutOfMemoryException in the preview pane (#16051)
1 parent 660a51e commit 8f57ed4

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/Files.App/UserControls/FilePreviews/MediaPreview.xaml.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
using CommunityToolkit.Mvvm.DependencyInjection;
21
using Files.App.ViewModels.Previews;
3-
using Files.App.Services.Settings;
42
using Microsoft.UI.Xaml;
53
using Microsoft.UI.Xaml.Controls;
64
using Microsoft.UI.Xaml.Input;
7-
using System;
85
using Windows.Media.Playback;
96

107
// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236
@@ -20,6 +17,7 @@ public MediaPreview(MediaPreviewViewModel model)
2017
ViewModel = model;
2118
InitializeComponent();
2219
PlayerContext.Loaded += PlayerContext_Loaded;
20+
Unloaded += MediaPreview_Unloaded;
2321
}
2422

2523
public MediaPreviewViewModel ViewModel { get; set; }
@@ -31,6 +29,16 @@ private void PlayerContext_Loaded(object sender, RoutedEventArgs e)
3129
ViewModel.TogglePlaybackRequested += TogglePlaybackRequestInvoked;
3230
}
3331

32+
private void MediaPreview_Unloaded(object sender, RoutedEventArgs e)
33+
{
34+
// The MediaPlayerElement isn't properly disposed by Windows so we set the source to null
35+
// to avoid issues the next time the control is used.
36+
PlayerContext.Source = null;
37+
38+
PlayerContext.Loaded -= PlayerContext_Loaded;
39+
Unloaded -= MediaPreview_Unloaded;
40+
}
41+
3442
private void MediaPlayer_VolumeChanged(MediaPlayer sender, object args)
3543
{
3644
if (sender.Volume != UserSettingsService.InfoPaneSettingsService.MediaVolume)

0 commit comments

Comments
 (0)