From fc8de8aeadaa74b691ec909f9481da9a6d90fd2e Mon Sep 17 00:00:00 2001 From: Bond-009 Date: Mon, 25 Mar 2019 17:27:24 +0100 Subject: [PATCH 1/2] Check if disposed first --- Emby.Dlna/PlayTo/PlayToController.cs | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/Emby.Dlna/PlayTo/PlayToController.cs b/Emby.Dlna/PlayTo/PlayToController.cs index 67d5cfef4..0808c77d1 100644 --- a/Emby.Dlna/PlayTo/PlayToController.cs +++ b/Emby.Dlna/PlayTo/PlayToController.cs @@ -102,9 +102,10 @@ namespace Emby.Dlna.PlayTo { _sessionManager.ReportSessionEnded(_session.Id); } - catch + catch (Exception ex) { // Could throw if the session is already gone + _logger.LogError(ex, "Error reporting the end of session {Id}", _session.Id); } } @@ -112,20 +113,14 @@ namespace Emby.Dlna.PlayTo { var info = e.Argument; - info.Headers.TryGetValue("NTS", out string nts); - - if (!info.Headers.TryGetValue("USN", out string usn)) usn = string.Empty; - - if (!info.Headers.TryGetValue("NT", out string nt)) nt = string.Empty; - - if (usn.IndexOf(_device.Properties.UUID, StringComparison.OrdinalIgnoreCase) != -1 && - !_disposed) + if (!_disposed + && info.Headers.TryGetValue("USN", out string usn) + && usn.IndexOf(_device.Properties.UUID, StringComparison.OrdinalIgnoreCase) != -1 + && (usn.IndexOf("MediaRenderer:", StringComparison.OrdinalIgnoreCase) != -1 + || (info.Headers.TryGetValue("NT", out string nt) + && nt.IndexOf("MediaRenderer:", StringComparison.OrdinalIgnoreCase) != -1))) { - if (usn.IndexOf("MediaRenderer:", StringComparison.OrdinalIgnoreCase) != -1 || - nt.IndexOf("MediaRenderer:", StringComparison.OrdinalIgnoreCase) != -1) - { - OnDeviceUnavailable(); - } + OnDeviceUnavailable(); } } From d623f616fa01abf5c0d0625eaf5c17c4acf86803 Mon Sep 17 00:00:00 2001 From: Bond-009 Date: Mon, 25 Mar 2019 17:32:27 +0100 Subject: [PATCH 2/2] Improved dispose method --- Emby.Dlna/PlayTo/PlayToController.cs | 40 ++++++++++++++++++---------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/Emby.Dlna/PlayTo/PlayToController.cs b/Emby.Dlna/PlayTo/PlayToController.cs index 0808c77d1..c58f16438 100644 --- a/Emby.Dlna/PlayTo/PlayToController.cs +++ b/Emby.Dlna/PlayTo/PlayToController.cs @@ -607,22 +607,34 @@ namespace Emby.Dlna.PlayTo public void Dispose() { - if (!_disposed) - { - _disposed = true; - - _device.PlaybackStart -= _device_PlaybackStart; - _device.PlaybackProgress -= _device_PlaybackProgress; - _device.PlaybackStopped -= _device_PlaybackStopped; - _device.MediaChanged -= _device_MediaChanged; - //_deviceDiscovery.DeviceLeft -= _deviceDiscovery_DeviceLeft; - _device.OnDeviceUnavailable = null; - - _device.Dispose(); - } + Dispose(true); + GC.SuppressFinalize(this); } - private readonly CultureInfo _usCulture = new CultureInfo("en-US"); + protected virtual void Dispose(bool disposing) + { + if (_disposed) + { + return; + } + + if (disposing) + { + _device.Dispose(); + } + + _device.PlaybackStart -= _device_PlaybackStart; + _device.PlaybackProgress -= _device_PlaybackProgress; + _device.PlaybackStopped -= _device_PlaybackStopped; + _device.MediaChanged -= _device_MediaChanged; + _deviceDiscovery.DeviceLeft -= _deviceDiscovery_DeviceLeft; + _device.OnDeviceUnavailable = null; + _device = null; + + _disposed = true; + } + + private static readonly CultureInfo _usCulture = CultureInfo.ReadOnly(new CultureInfo("en-US")); private Task SendGeneralCommand(GeneralCommand command, CancellationToken cancellationToken) {