From 468347632a5cb773e63c7c8b1f63f544f559a2c7 Mon Sep 17 00:00:00 2001 From: Ryan Molden Date: Thu, 1 Feb 2024 16:13:27 -0800 Subject: [PATCH] Prevent leaking WPF textview --- doki-theme-visualstudio/StickerAdornment.cs | 22 ++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/doki-theme-visualstudio/StickerAdornment.cs b/doki-theme-visualstudio/StickerAdornment.cs index 29eca32..4d4f31c 100644 --- a/doki-theme-visualstudio/StickerAdornment.cs +++ b/doki-theme-visualstudio/StickerAdornment.cs @@ -13,7 +13,7 @@ internal sealed class StickerAdornment { private const string TagName = "DokiSticker"; - private readonly IWpfTextView _view; + private IWpfTextView _view; private bool _registeredLayoutListener; @@ -25,7 +25,7 @@ public StickerAdornment(IWpfTextView view) { _adornmentLayer = view.GetAdornmentLayer("StickerAdornment"); RemoveAdornment(); - ThemeManager.Instance.DokiThemeChanged += (_, themeChangedArgs) => { + EventHandler themeChangedCallback = (_, themeChangedArgs) => { var newDokiTheme = themeChangedArgs.Theme; if (newDokiTheme != null && SettingsService.Instance.DrawSticker) { GetImageSource(newDokiTheme, newSource => { @@ -38,7 +38,9 @@ public StickerAdornment(IWpfTextView view) { } }; - SettingsService.Instance.SettingsChanged += (_, service) => { + ThemeManager.Instance.DokiThemeChanged += themeChangedCallback; + + EventHandler settingsChangedCallback = (_, service) => { if (service.DrawSticker) { DrawCurrentSticker(); DoStupidShit(); @@ -49,6 +51,20 @@ public StickerAdornment(IWpfTextView view) { _stickerSize = service.StickerRelativeSize; }; + SettingsService.Instance.SettingsChanged += settingsChangedCallback; + + EventHandler textviewClosed = null; + textviewClosed = (_, args) => { + // Clean up all our references as the textview has closed + ThemeManager.Instance.DokiThemeChanged -= themeChangedCallback; + SettingsService.Instance.SettingsChanged -= settingsChangedCallback; + + _view.Closed -= textviewClosed; + _view = null; + }; + + _view.Closed += textviewClosed; + _stickerSize = SettingsService.Instance.StickerRelativeSize; if (!SettingsService.Instance.DrawSticker) return;