From df00f4176501207dc2d736a4b7ea4e1dea262239 Mon Sep 17 00:00:00 2001 From: ema Date: Fri, 20 Dec 2024 22:48:30 +0800 Subject: [PATCH] Safely disposing GIF-related memory resources --- .../AnimatedImage/Providers/GifProvider.cs | 21 +++++++++---------- .../MetaProvider.cs | 3 +-- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/AnimatedImage/Providers/GifProvider.cs b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/AnimatedImage/Providers/GifProvider.cs index 5174fe20..edde70f9 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/AnimatedImage/Providers/GifProvider.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/AnimatedImage/Providers/GifProvider.cs @@ -19,6 +19,7 @@ using QuickLook.Common.Helpers; using QuickLook.Common.Plugin; using System; +using System.Diagnostics; using System.Drawing; using System.Drawing.Imaging; using System.IO; @@ -43,9 +44,9 @@ internal class GifProvider : AnimationProvider private NativeProvider _nativeProvider; private int[] _frameDelays; // in millisecond - private int _frameCount = 0; + private readonly int _frameCount = 0; private int _frameIndex = 0; - private int _maxLoopCount = 0; // 0 - infinite loop + private readonly int _maxLoopCount = 0; // 0 - infinite loop private int _loopIndex = 0; private TimeSpan _minTickTimeInMillisecond = TimeSpan.FromMilliseconds(20); @@ -95,20 +96,18 @@ public override void Dispose() try { - if (_bitmap != null) + lock (_bitmap ?? new object()) // Lock to prevent null reference exception { - lock (_bitmap) - { - _stream?.Dispose(); - _bitmap?.Dispose(); + _bitmap?.Dispose(); + _bitmap = null; - _bitmap = null; - _stream = null; - } + _stream?.Dispose(); + _stream = null; } } - catch + catch (Exception e) { + Debug.WriteLine(e); } _frame = null; diff --git a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/MetaProvider.cs b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/MetaProvider.cs index 4bc2aafd..91446c33 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/MetaProvider.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/MetaProvider.cs @@ -28,8 +28,7 @@ namespace QuickLook.Plugin.ImageViewer; public class MetaProvider { - private readonly SortedDictionary _cache = - new SortedDictionary(); // [key, [label, value]] + private readonly SortedDictionary _cache = []; // [key, [label, value]] private readonly string _path;