Fix native memory leak
in GC language.
This commit is contained in:
parent
d0862ab454
commit
d782b33970
@ -69,7 +69,7 @@ fun main() {
|
|||||||
for (chunkX in 0 .. 100) {
|
for (chunkX in 0 .. 100) {
|
||||||
//for (chunkX in 0 .. 17) {
|
//for (chunkX in 0 .. 17) {
|
||||||
// for (chunkY in 21 .. 21) {
|
// for (chunkY in 21 .. 21) {
|
||||||
for (chunkY in 18 .. 24) {
|
for (chunkY in 0 .. 24) {
|
||||||
val data = db.read(byteArrayOf(1, (chunkX shr 8).toByte(), chunkX.toByte(), (chunkY shr 8).toByte(), chunkY.toByte()))
|
val data = db.read(byteArrayOf(1, (chunkX shr 8).toByte(), chunkX.toByte(), (chunkY shr 8).toByte(), chunkY.toByte()))
|
||||||
val data2 = db.read(byteArrayOf(2, (chunkX shr 8).toByte(), chunkX.toByte(), (chunkY shr 8).toByte(), chunkY.toByte()))
|
val data2 = db.read(byteArrayOf(2, (chunkX shr 8).toByte(), chunkX.toByte(), (chunkY shr 8).toByte(), chunkY.toByte()))
|
||||||
|
|
||||||
|
@ -16,6 +16,11 @@ import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap
|
|||||||
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap
|
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap
|
||||||
import org.apache.logging.log4j.LogManager
|
import org.apache.logging.log4j.LogManager
|
||||||
import org.lwjgl.opengl.GL45
|
import org.lwjgl.opengl.GL45
|
||||||
|
import org.lwjgl.stb.STBIEOFCallback
|
||||||
|
import org.lwjgl.stb.STBIIOCallbacks
|
||||||
|
import org.lwjgl.stb.STBIReadCallback
|
||||||
|
import org.lwjgl.stb.STBIReadCallbackI
|
||||||
|
import org.lwjgl.stb.STBISkipCallback
|
||||||
import org.lwjgl.stb.STBImage
|
import org.lwjgl.stb.STBImage
|
||||||
import org.lwjgl.system.MemoryUtil
|
import org.lwjgl.system.MemoryUtil
|
||||||
import ru.dbotthepony.kstarbound.world.PIXELS_IN_STARBOUND_UNIT
|
import ru.dbotthepony.kstarbound.world.PIXELS_IN_STARBOUND_UNIT
|
||||||
@ -25,7 +30,6 @@ import ru.dbotthepony.kstarbound.api.IStarboundFile
|
|||||||
import ru.dbotthepony.kstarbound.client.StarboundClient
|
import ru.dbotthepony.kstarbound.client.StarboundClient
|
||||||
import ru.dbotthepony.kstarbound.client.gl.GLTexture2D
|
import ru.dbotthepony.kstarbound.client.gl.GLTexture2D
|
||||||
import ru.dbotthepony.kstarbound.io.json.consumeNull
|
import ru.dbotthepony.kstarbound.io.json.consumeNull
|
||||||
import ru.dbotthepony.kstarbound.io.stream2STBIO
|
|
||||||
import ru.dbotthepony.kstarbound.util.contains
|
import ru.dbotthepony.kstarbound.util.contains
|
||||||
import ru.dbotthepony.kstarbound.util.get
|
import ru.dbotthepony.kstarbound.util.get
|
||||||
import ru.dbotthepony.kstarbound.util.getObject
|
import ru.dbotthepony.kstarbound.util.getObject
|
||||||
@ -341,12 +345,42 @@ class Image private constructor(
|
|||||||
val getHeight = intArrayOf(0)
|
val getHeight = intArrayOf(0)
|
||||||
val components = intArrayOf(0)
|
val components = intArrayOf(0)
|
||||||
|
|
||||||
|
val stream = BufferedInputStream(file.open())
|
||||||
|
val callback = STBIIOCallbacks.malloc()
|
||||||
|
|
||||||
|
val readCallback = STBIReadCallback.create(STBIReadCallbackI { _, buf, size ->
|
||||||
|
val readBuf = ByteArray(size)
|
||||||
|
val read = stream.read(readBuf)
|
||||||
|
|
||||||
|
for (i in 0 until read) {
|
||||||
|
MemoryUtil.memPutByte(buf + i, readBuf[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
return@STBIReadCallbackI read
|
||||||
|
})
|
||||||
|
|
||||||
|
val skipCallback = STBISkipCallback.create { _, n -> stream.skip(n.toLong()) }
|
||||||
|
|
||||||
|
val eofCallback = STBIEOFCallback.create {
|
||||||
|
stream.mark(1)
|
||||||
|
val empty = stream.read() == -1
|
||||||
|
stream.reset()
|
||||||
|
if (empty) 1 else 0
|
||||||
|
}
|
||||||
|
|
||||||
|
callback.set(readCallback, skipCallback, eofCallback)
|
||||||
|
|
||||||
val status = STBImage.stbi_info_from_callbacks(
|
val status = STBImage.stbi_info_from_callbacks(
|
||||||
stream2STBIO(BufferedInputStream(file.open())), 0L,
|
callback, 0L,
|
||||||
getWidth, getHeight,
|
getWidth, getHeight,
|
||||||
components
|
components
|
||||||
)
|
)
|
||||||
|
|
||||||
|
readCallback.free()
|
||||||
|
skipCallback.free()
|
||||||
|
eofCallback.free()
|
||||||
|
callback.free()
|
||||||
|
|
||||||
if (!status)
|
if (!status)
|
||||||
throw IllegalArgumentException("File $file is not an image or it is corrupted")
|
throw IllegalArgumentException("File $file is not an image or it is corrupted")
|
||||||
|
|
||||||
|
@ -1,38 +0,0 @@
|
|||||||
package ru.dbotthepony.kstarbound.io
|
|
||||||
|
|
||||||
import org.lwjgl.stb.STBIEOFCallback
|
|
||||||
import org.lwjgl.stb.STBIIOCallbacks
|
|
||||||
import org.lwjgl.stb.STBIReadCallback
|
|
||||||
import org.lwjgl.stb.STBIReadCallbackI
|
|
||||||
import org.lwjgl.stb.STBISkipCallback
|
|
||||||
import org.lwjgl.system.MemoryUtil
|
|
||||||
import java.io.InputStream
|
|
||||||
|
|
||||||
fun stream2STBIO(stream: InputStream): STBIIOCallbacks {
|
|
||||||
require(stream.markSupported()) { "Provided stream must support mark" }
|
|
||||||
|
|
||||||
val callback = STBIIOCallbacks.create()
|
|
||||||
|
|
||||||
val read = STBIReadCallback.create(STBIReadCallbackI { _, buf, size ->
|
|
||||||
val readBuf = ByteArray(size)
|
|
||||||
val read = stream.read(readBuf)
|
|
||||||
|
|
||||||
for (i in 0 until read) {
|
|
||||||
MemoryUtil.memPutByte(buf + i, readBuf[i])
|
|
||||||
}
|
|
||||||
|
|
||||||
return@STBIReadCallbackI read
|
|
||||||
})
|
|
||||||
|
|
||||||
val skip = STBISkipCallback.create { _, n -> stream.skip(n.toLong()) }
|
|
||||||
|
|
||||||
val eof = STBIEOFCallback.create {
|
|
||||||
stream.mark(1)
|
|
||||||
val empty = stream.read() == -1
|
|
||||||
stream.reset()
|
|
||||||
if (empty) 1 else 0
|
|
||||||
}
|
|
||||||
|
|
||||||
callback.set(read, skip, eof)
|
|
||||||
return callback
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user