From bd8d0e58dfe4364e131ed48132f8c0dd5a10b7e7 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 2 Feb 2024 15:47:42 +0700 Subject: [PATCH] "Fix" random crash inside stbi_load_from_memory caused by JVM releasing DirectByteBuffer while it is being used --- .../kotlin/ru/dbotthepony/kstarbound/defs/image/Image.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/image/Image.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/image/Image.kt index 6f7cd3ae..4d50d168 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/image/Image.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/image/Image.kt @@ -315,12 +315,16 @@ class Image private constructor( val getHeight = intArrayOf(0) val components = intArrayOf(0) + val idata = it.readDirect() + val data = STBImage.stbi_load_from_memory( - it.readDirect(), + idata, getWidth, getHeight, components, 0 ) ?: throw IllegalArgumentException("File $it is not an image or it is corrupted") + synchronized(idata) {} // memory barrier + val address = MemoryUtil.memAddress(data) Starbound.CLEANER.register(data) { STBImage.nstbi_image_free(address) }