no more access violation from freeing already freed memory

This commit is contained in:
minjaesong
2019-06-08 03:29:38 +09:00
parent 15cb42e26b
commit 30ae587554
3 changed files with 11 additions and 2 deletions

View File

@@ -16,6 +16,7 @@ open class BlockLayer(val width: Int, val height: Int) : Disposable {
unsafe = unsafeConstructor.newInstance()
}
private var unsafeArrayInitialised = false
private var unsafeArrayDestroyed = false
private var layerPtr = unsafe.allocateMemory(width * height * BYTES_PER_BLOCK.toLong())
@@ -135,8 +136,11 @@ open class BlockLayer(val width: Int, val height: Int) : Disposable {
fun isInBound(x: Int, y: Int) = (x >= 0 && y >= 0 && x < width && y < height)
override fun dispose() {
unsafe.freeMemory(layerPtr)
printdbg(this, "BlockLayer successfully freed")
if (!unsafeArrayDestroyed) {
unsafe.freeMemory(layerPtr)
unsafeArrayDestroyed = true
printdbg(this, "BlockLayer successfully freed")
}
}
companion object {