From 89d1582da8ead8de9dc21f9990a5a0a16e9229e8 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Thu, 28 Apr 2022 14:55:50 +0200 Subject: [PATCH] Add missing dispose of webpInfo --- .../Formats/Webp/WebpAnimationDecoder.cs | 27 ++++++++++++++----- .../MemoryAllocatorValidator.cs | 6 ++--- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/ImageSharp/Formats/Webp/WebpAnimationDecoder.cs b/src/ImageSharp/Formats/Webp/WebpAnimationDecoder.cs index 16589e50f1..fd16b3d268 100644 --- a/src/ImageSharp/Formats/Webp/WebpAnimationDecoder.cs +++ b/src/ImageSharp/Formats/Webp/WebpAnimationDecoder.cs @@ -248,16 +248,29 @@ namespace SixLabors.ImageSharp.Formats.Webp where TPixel : unmanaged, IPixel { var decodedImage = new Image((int)frameData.Width, (int)frameData.Height); - Buffer2D pixelBufferDecoded = decodedImage.Frames.RootFrame.PixelBuffer; - if (webpInfo.IsLossless) + + try + { + Buffer2D pixelBufferDecoded = decodedImage.Frames.RootFrame.PixelBuffer; + if (webpInfo.IsLossless) + { + var losslessDecoder = new WebpLosslessDecoder(webpInfo.Vp8LBitReader, this.memoryAllocator, this.configuration); + losslessDecoder.Decode(pixelBufferDecoded, (int)webpInfo.Width, (int)webpInfo.Height); + } + else + { + var lossyDecoder = new WebpLossyDecoder(webpInfo.Vp8BitReader, this.memoryAllocator, this.configuration); + lossyDecoder.Decode(pixelBufferDecoded, (int)webpInfo.Width, (int)webpInfo.Height, webpInfo, this.AlphaData); + } + } + catch { - var losslessDecoder = new WebpLosslessDecoder(webpInfo.Vp8LBitReader, this.memoryAllocator, this.configuration); - losslessDecoder.Decode(pixelBufferDecoded, (int)webpInfo.Width, (int)webpInfo.Height); + decodedImage?.Dispose(); + throw; } - else + finally { - var lossyDecoder = new WebpLossyDecoder(webpInfo.Vp8BitReader, this.memoryAllocator, this.configuration); - lossyDecoder.Decode(pixelBufferDecoded, (int)webpInfo.Width, (int)webpInfo.Height, webpInfo, this.AlphaData); + webpInfo.Dispose(); } return decodedImage; diff --git a/tests/ImageSharp.Tests/MemoryAllocatorValidator.cs b/tests/ImageSharp.Tests/MemoryAllocatorValidator.cs index 13664ee9b2..cdd6754cf1 100644 --- a/tests/ImageSharp.Tests/MemoryAllocatorValidator.cs +++ b/tests/ImageSharp.Tests/MemoryAllocatorValidator.cs @@ -59,9 +59,9 @@ namespace SixLabors.ImageSharp.Tests public void Validate(int expectedAllocationCount) { - var count = this.TotalRemainingAllocated; - var pass = expectedAllocationCount == count; - Assert.True(pass, $"Expected a {expectedAllocationCount} undisposed buffers but found {count}"); + int count = this.TotalRemainingAllocated; + bool pass = expectedAllocationCount == count; + Assert.True(pass, $"Expected {expectedAllocationCount} undisposed buffers but found {count}"); } public void Dispose()