diff src/name/blackcap/imageprep/Menus.kt @ 15:fad32eda667f

Fix ImageWriter leak.
author David Barts <n5jrn@me.com>
date Sun, 19 Jul 2020 13:49:23 -0700
parents bed255e4c2dc
children d71523cde521
line wrap: on
line diff
--- a/src/name/blackcap/imageprep/Menus.kt	Sun Jul 19 13:32:02 2020 -0700
+++ b/src/name/blackcap/imageprep/Menus.kt	Sun Jul 19 13:49:23 2020 -0700
@@ -147,12 +147,16 @@
                     /* xxx - end workaround */
                     ios.use {
                         val writer = ImageIO.getImageWritersByFormatName("jpeg").next()
-                        val iwp = writer.getDefaultWriteParam().apply {
-                            setCompressionMode(ImageWriteParam.MODE_EXPLICIT)
-                            setCompressionQuality(Settings.outputQuality.toFloat() / 100.0f)
+                        try {
+                            val iwp = writer.getDefaultWriteParam().apply {
+                                setCompressionMode(ImageWriteParam.MODE_EXPLICIT)
+                                setCompressionQuality(Settings.outputQuality.toFloat() / 100.0f)
+                            }
+                            writer.setOutput(it)
+                            writer.write(null, IIOImage(w.image, null, null), iwp)
+                        } finally {
+                            writer.dispose()
                         }
-                        writer.setOutput(it)
-                        writer.write(null, IIOImage(w.image, null, null), iwp)
                     }
                     null
                 } catch (e: IOException) {