Fix Lua memory leaks related to dangling handles
This commit is contained in:
parent
88eb691045
commit
d46ffdb66b
@ -1,6 +1,10 @@
|
||||
package ru.dbotthepony.kstarbound.lua
|
||||
|
||||
data class CommonHandleRegistry(
|
||||
val future: LuaHandle,
|
||||
val pathFinder: LuaHandle,
|
||||
)
|
||||
val future: LuaHandle = LuaHandle.Nil,
|
||||
val pathFinder: LuaHandle = LuaHandle.Nil,
|
||||
) {
|
||||
companion object {
|
||||
val EMPTY = CommonHandleRegistry()
|
||||
}
|
||||
}
|
||||
|
@ -176,10 +176,10 @@ fun LuaThread.getVector2iOrAABB(stackIndex: Int = -1): Either<Vector2i, AABB>? {
|
||||
|
||||
fun LuaThread.ArgStack.nextVector2iOrAABB(position: Int = this.position++): Either<Vector2i, AABB> {
|
||||
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)
|
||||
?: 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>? {
|
||||
|
@ -7,6 +7,7 @@ import ru.dbotthepony.kstarbound.Starbound
|
||||
import ru.dbotthepony.kstarbound.json.InternedJsonElementAdapter
|
||||
import java.io.Closeable
|
||||
import java.lang.ref.Cleaner.Cleanable
|
||||
import java.lang.ref.WeakReference
|
||||
|
||||
interface LuaHandle : Closeable {
|
||||
fun push(into: LuaThread)
|
||||
@ -127,12 +128,12 @@ interface LuaHandle : Closeable {
|
||||
}
|
||||
|
||||
init {
|
||||
val parent = parent
|
||||
val parent = WeakReference(parent)
|
||||
val handle = handle
|
||||
val key = key
|
||||
|
||||
cleanable = Starbound.CLEANER.register(this) {
|
||||
parent.freeHandle(handle, key)
|
||||
parent.get()?.freeHandle(handle, key)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -45,6 +45,9 @@ class LuaSharedState(val handlesThread: LuaThread, private val cleanable: Cleana
|
||||
isValid = false
|
||||
namedHandles.clear()
|
||||
cleanable.clean()
|
||||
errorToStringFunction = LuaHandle.Nil
|
||||
errorTrapFunction = LuaHandle.Nil
|
||||
commonHandles = CommonHandleRegistry.EMPTY
|
||||
}
|
||||
|
||||
fun initializeHandles(mainThread: LuaThread) {
|
||||
|
@ -406,10 +406,10 @@ class MonsterEntity(val variant: MonsterVariant, level: Double? = null) : ActorE
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
luaUpdate.update(delta) {
|
||||
/*luaUpdate.update(delta) {
|
||||
luaMovement.clearControlsIfNeeded()
|
||||
forceRegions.clear()
|
||||
}
|
||||
}*/
|
||||
} catch (err: Exception) {
|
||||
LOGGER.error("Exception while ticking $this", err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user