From a4f6f64a5244b1edaf2cf3813421c37fbdde627c Mon Sep 17 00:00:00 2001 From: Gauthier Roebroeck Date: Thu, 1 Aug 2024 10:53:46 +0800 Subject: [PATCH] fix(heif): properly clean image resource Closes: #55 --- .../main/java21/com/github/gotson/nightmonkeys/heif/Heif.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/imageio-heif/src/main/java21/com/github/gotson/nightmonkeys/heif/Heif.java b/imageio-heif/src/main/java21/com/github/gotson/nightmonkeys/heif/Heif.java index 9a73e37..d72f617 100644 --- a/imageio-heif/src/main/java21/com/github/gotson/nightmonkeys/heif/Heif.java +++ b/imageio-heif/src/main/java21/com/github/gotson/nightmonkeys/heif/Heif.java @@ -113,6 +113,7 @@ public static void decode(final ImageInputStream stream, BasicInfo info, final W MemorySegment heifContext = null; MemorySegment handle = null; + MemorySegment image = null; try { heif_h.heif_init(arena, MemorySegment.NULL); heifContext = heif_h.heif_context_alloc(); @@ -132,7 +133,7 @@ public static void decode(final ImageInputStream stream, BasicInfo info, final W checkError( heif_h.heif_decode_image(arena, handle, imagePtr, HeifColorSpace.HEIF_COLOR_SPACE_RGB.intValue(), HeifChroma.HEIF_CHROMA_INTERLEAVED_RGBA.intValue(), MemorySegment.NULL)); - var image = imagePtr.get(C_POINTER, 0); + image = imagePtr.get(C_POINTER, 0); var stridePtr = arena.allocate(C_INT); var pixels = heif_h.heif_image_get_plane_readonly(image, heif_channel_interleaved(), stridePtr); @@ -163,6 +164,7 @@ public static void decode(final ImageInputStream stream, BasicInfo info, final W raster.setDataElements(0, 0, raster.getWidth(), raster.getHeight(), pixelsRaster); } } finally { + if (image != null) heif_h.heif_image_release(image); if (heifContext != null) heif_h.heif_context_free(heifContext); if (handle != null) heif_h.heif_image_handle_release(handle); heif_h.heif_deinit();