Fix Lua memory leaks related to dangling handles

This commit is contained in:
DBotThePony 2024-12-29 20:49:11 +07:00
parent 88eb691045
commit d46ffdb66b
Signed by: DBot
GPG Key ID: DCC23B5715498507
5 changed files with 17 additions and 9 deletions

View File

@ -1,6 +1,10 @@
package ru.dbotthepony.kstarbound.lua package ru.dbotthepony.kstarbound.lua
data class CommonHandleRegistry( data class CommonHandleRegistry(
val future: LuaHandle, val future: LuaHandle = LuaHandle.Nil,
val pathFinder: LuaHandle, val pathFinder: LuaHandle = LuaHandle.Nil,
) ) {
companion object {
val EMPTY = CommonHandleRegistry()
}
}

View File

@ -176,10 +176,10 @@ fun LuaThread.getVector2iOrAABB(stackIndex: Int = -1): Either<Vector2i, AABB>? {
fun LuaThread.ArgStack.nextVector2iOrAABB(position: Int = this.position++): Either<Vector2i, AABB> { fun LuaThread.ArgStack.nextVector2iOrAABB(position: Int = this.position++): Either<Vector2i, AABB> {
if (position !in 1 ..this.top) if (position !in 1 ..this.top)
throw IllegalArgumentException("bad argument #$position: Vector2d expected, got nil") throw IllegalArgumentException("bad argument #$position: Vector2i expected, got nil")
return lua.getVector2iOrAABB(position) return lua.getVector2iOrAABB(position)
?: throw IllegalArgumentException("bad argument #$position: Vector2d or AABB expected, got ${lua.typeAt(position)}") ?: throw IllegalArgumentException("bad argument #$position: Vector2i or AABB expected, got ${lua.typeAt(position)}")
} }
fun LuaThread.ArgStack.nextOptionalVector2iOrAABB(position: Int = this.position++): Either<Vector2i, AABB>? { fun LuaThread.ArgStack.nextOptionalVector2iOrAABB(position: Int = this.position++): Either<Vector2i, AABB>? {

View File

@ -7,6 +7,7 @@ import ru.dbotthepony.kstarbound.Starbound
import ru.dbotthepony.kstarbound.json.InternedJsonElementAdapter import ru.dbotthepony.kstarbound.json.InternedJsonElementAdapter
import java.io.Closeable import java.io.Closeable
import java.lang.ref.Cleaner.Cleanable import java.lang.ref.Cleaner.Cleanable
import java.lang.ref.WeakReference
interface LuaHandle : Closeable { interface LuaHandle : Closeable {
fun push(into: LuaThread) fun push(into: LuaThread)
@ -127,12 +128,12 @@ interface LuaHandle : Closeable {
} }
init { init {
val parent = parent val parent = WeakReference(parent)
val handle = handle val handle = handle
val key = key val key = key
cleanable = Starbound.CLEANER.register(this) { cleanable = Starbound.CLEANER.register(this) {
parent.freeHandle(handle, key) parent.get()?.freeHandle(handle, key)
} }
} }

View File

@ -45,6 +45,9 @@ class LuaSharedState(val handlesThread: LuaThread, private val cleanable: Cleana
isValid = false isValid = false
namedHandles.clear() namedHandles.clear()
cleanable.clean() cleanable.clean()
errorToStringFunction = LuaHandle.Nil
errorTrapFunction = LuaHandle.Nil
commonHandles = CommonHandleRegistry.EMPTY
} }
fun initializeHandles(mainThread: LuaThread) { fun initializeHandles(mainThread: LuaThread) {

View File

@ -406,10 +406,10 @@ class MonsterEntity(val variant: MonsterVariant, level: Double? = null) : ActorE
} }
} else { } else {
try { try {
luaUpdate.update(delta) { /*luaUpdate.update(delta) {
luaMovement.clearControlsIfNeeded() luaMovement.clearControlsIfNeeded()
forceRegions.clear() forceRegions.clear()
} }*/
} catch (err: Exception) { } catch (err: Exception) {
LOGGER.error("Exception while ticking $this", err) LOGGER.error("Exception while ticking $this", err)
} }