From f662c062d21b3952025407415cba2f6f441eabd3 Mon Sep 17 00:00:00 2001 From: Mykhailo Pylyp Date: Fri, 18 Jun 2021 13:40:10 +0300 Subject: [PATCH] [Run-Plugin][Program] Fix null reference exception on Dispose (#11785) --- .../Community.PowerToys.Run.Plugin.UnitConverter/Main.cs | 6 +++++- .../launcher/Plugins/Microsoft.Plugin.Folder/Main.cs | 6 +++++- .../launcher/Plugins/Microsoft.Plugin.Program/Main.cs | 8 ++++++-- src/modules/launcher/Plugins/Microsoft.Plugin.Uri/Main.cs | 6 +++++- .../Microsoft.PowerToys.Run.Plugin.Calculator/Main.cs | 6 +++++- .../Microsoft.PowerToys.Run.Plugin.Registry/Main.cs | 2 +- .../Main.cs | 2 +- 7 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.UnitConverter/Main.cs b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.UnitConverter/Main.cs index 2c59859693..9d6478a10d 100644 --- a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.UnitConverter/Main.cs +++ b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.UnitConverter/Main.cs @@ -171,7 +171,11 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter { if (disposing) { - _context.API.ThemeChanged -= OnThemeChanged; + if (_context != null && _context.API != null) + { + _context.API.ThemeChanged -= OnThemeChanged; + } + _disposed = true; } } diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Folder/Main.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.Folder/Main.cs index 154b822d6b..b48195fa2e 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Folder/Main.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Folder/Main.cs @@ -134,7 +134,11 @@ namespace Microsoft.Plugin.Folder { if (disposing) { - _context.API.ThemeChanged -= OnThemeChanged; + if (_context != null && _context.API != null) + { + _context.API.ThemeChanged -= OnThemeChanged; + } + _disposed = true; } } diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Main.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Main.cs index 0e8ec09024..4d078cb0af 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Main.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Main.cs @@ -210,8 +210,12 @@ namespace Microsoft.Plugin.Program { if (disposing) { - _context.API.ThemeChanged -= OnThemeChanged; - _win32ProgramRepositoryHelper.Dispose(); + if (_context != null && _context.API != null) + { + _context.API.ThemeChanged -= OnThemeChanged; + } + + _win32ProgramRepositoryHelper?.Dispose(); _disposed = true; } } diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Uri/Main.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.Uri/Main.cs index a069b5b5c3..a1ea328fa5 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Uri/Main.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Uri/Main.cs @@ -226,7 +226,11 @@ namespace Microsoft.Plugin.Uri { if (!_disposed && disposing) { - Context.API.ThemeChanged -= OnThemeChanged; + if (Context != null && Context.API != null) + { + Context.API.ThemeChanged -= OnThemeChanged; + } + _disposed = true; } } diff --git a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/Main.cs b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/Main.cs index 822531f2c4..0dba5f28a4 100644 --- a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/Main.cs +++ b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/Main.cs @@ -113,7 +113,11 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator { if (disposing) { - Context.API.ThemeChanged -= OnThemeChanged; + if (Context != null && Context.API != null) + { + Context.API.ThemeChanged -= OnThemeChanged; + } + _disposed = true; } } diff --git a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Registry/Main.cs b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Registry/Main.cs index 2e5cc81bf7..9408988f2d 100644 --- a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Registry/Main.cs +++ b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Registry/Main.cs @@ -154,7 +154,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Registry return; } - if (!(_context is null)) + if (_context != null && _context.API != null) { _context.API.ThemeChanged -= OnThemeChanged; } diff --git a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.WindowsSettings/Main.cs b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.WindowsSettings/Main.cs index dfc9b22964..9c6fabbf1d 100644 --- a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.WindowsSettings/Main.cs +++ b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.WindowsSettings/Main.cs @@ -169,7 +169,7 @@ namespace Microsoft.PowerToys.Run.Plugin.WindowsSettings return; } - if (!(_context is null)) + if (_context != null && _context.API != null) { _context.API.ThemeChanged -= OnThemeChanged; }