diff --git a/src/net/torvald/UnsafePtr.kt b/src/net/torvald/UnsafePtr.kt index 91e7cd4e7..17696b4ea 100644 --- a/src/net/torvald/UnsafePtr.kt +++ b/src/net/torvald/UnsafePtr.kt @@ -32,12 +32,16 @@ class UnsafePtr(val ptr: Long, val allocSize: Long) { fun destroy() { if (!destroyed) { UnsafeHelper.unsafe.freeMemory(ptr) + + println("[UnsafePtr] Destroying pointer $this; called from:") + Thread.currentThread().stackTrace.forEach { println("[UnsafePtr] $it") } + destroyed = true } } private inline fun checkNullPtr(index: Long) { - if (destroyed) throw NullPointerException("The pointer is already destroyed (0x${ptr.toString(16)})") + if (destroyed) throw NullPointerException("The pointer is already destroyed ($this)") // OOB Check: debugging purposes only -- comment out for the production //if (index !in 0 until allocSize) throw IndexOutOfBoundsException("Index: $index; alloc size: $allocSize") @@ -67,4 +71,5 @@ class UnsafePtr(val ptr: Long, val allocSize: Long) { UnsafeHelper.unsafe.setMemory(ptr, allocSize, byte) } + override fun toString() = "0x${ptr.toString(16)} with size $allocSize" } \ No newline at end of file diff --git a/src/net/torvald/terrarum/TitleScreen.kt b/src/net/torvald/terrarum/TitleScreen.kt index 2900628c7..23d8f1553 100644 --- a/src/net/torvald/terrarum/TitleScreen.kt +++ b/src/net/torvald/terrarum/TitleScreen.kt @@ -253,7 +253,9 @@ class TitleScreen(val batch: SpriteBatch) : Screen { gdxClearAndSetBlend(.64f, .754f, .84f, 1f) - IngameRenderer.invoke(gamePaused = false, world = demoWorld, uisToDraw = uiContainer) + if (!demoWorld.disposed) { // FIXME q&d hack to circumvent the dangling pointer issue #26 + IngameRenderer.invoke(gamePaused = false, world = demoWorld, uisToDraw = uiContainer) + } batch.inUse { diff --git a/src/net/torvald/terrarum/gameworld/BlockLayer.kt b/src/net/torvald/terrarum/gameworld/BlockLayer.kt index f8301cc40..870a870dd 100644 --- a/src/net/torvald/terrarum/gameworld/BlockLayer.kt +++ b/src/net/torvald/terrarum/gameworld/BlockLayer.kt @@ -12,6 +12,7 @@ import net.torvald.terrarum.AppLoader.printdbg */ open class BlockLayer(val width: Int, val height: Int) : Disposable { + // using unsafe pointer gets you 100 fps, whereas using directbytebuffer gets you 90 private val ptr = UnsafeHelper.allocate(width * height * BYTES_PER_BLOCK) //private val directByteBuffer: ByteBuffer diff --git a/src/net/torvald/terrarum/gameworld/GameWorld.kt b/src/net/torvald/terrarum/gameworld/GameWorld.kt index 67e4a6af8..72530248f 100644 --- a/src/net/torvald/terrarum/gameworld/GameWorld.kt +++ b/src/net/torvald/terrarum/gameworld/GameWorld.kt @@ -93,6 +93,9 @@ open class GameWorld : Disposable { var generatorSeed: Long = 0 internal set + var disposed = false + private set + constructor(worldIndex: Int, width: Int, height: Int, creationTIME_T: Long, lastPlayTIME_T: Long, totalPlayTime: Int) { if (width <= 0 || height <= 0) throw IllegalArgumentException("Non-positive width/height: ($width, $height)") @@ -461,6 +464,8 @@ open class GameWorld : Disposable { layerWall.dispose() layerTerrain.dispose() //nullWorldInstance?.dispose() // must be called ONLY ONCE; preferably when the app exits + + disposed = true } companion object {