Safely disposing GIF-related memory resources

This commit is contained in:
ema
2024-12-20 22:48:30 +08:00
parent 5220b0b5d8
commit df00f41765
2 changed files with 11 additions and 13 deletions

View File

@@ -19,6 +19,7 @@ using QuickLook.Common.ExtensionMethods;
using QuickLook.Common.Helpers; using QuickLook.Common.Helpers;
using QuickLook.Common.Plugin; using QuickLook.Common.Plugin;
using System; using System;
using System.Diagnostics;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.IO; using System.IO;
@@ -43,9 +44,9 @@ internal class GifProvider : AnimationProvider
private NativeProvider _nativeProvider; private NativeProvider _nativeProvider;
private int[] _frameDelays; // in millisecond private int[] _frameDelays; // in millisecond
private int _frameCount = 0; private readonly int _frameCount = 0;
private int _frameIndex = 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 int _loopIndex = 0;
private TimeSpan _minTickTimeInMillisecond = TimeSpan.FromMilliseconds(20); private TimeSpan _minTickTimeInMillisecond = TimeSpan.FromMilliseconds(20);
@@ -95,20 +96,18 @@ internal class GifProvider : AnimationProvider
try try
{ {
if (_bitmap != null) lock (_bitmap ?? new object()) // Lock to prevent null reference exception
{ {
lock (_bitmap) _bitmap?.Dispose();
{ _bitmap = null;
_stream?.Dispose();
_bitmap?.Dispose();
_bitmap = null; _stream?.Dispose();
_stream = null; _stream = null;
}
} }
} }
catch catch (Exception e)
{ {
Debug.WriteLine(e);
} }
_frame = null; _frame = null;

View File

@@ -28,8 +28,7 @@ namespace QuickLook.Plugin.ImageViewer;
public class MetaProvider public class MetaProvider
{ {
private readonly SortedDictionary<string, (string, string)> _cache = private readonly SortedDictionary<string, (string, string)> _cache = []; // [key, [label, value]]
new SortedDictionary<string, (string, string)>(); // [key, [label, value]]
private readonly string _path; private readonly string _path;