Skip to content

Commit 4cfcf7b

Browse files
committed
Memory Leak: ChunkedESP processing chunks when disabled
This does not fully fix the core issue. We seem to not correctly free up unloaded chunks as well. Needs investigation.
1 parent 428f86a commit 4cfcf7b

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/main/kotlin/com/lambda/graphics/renderer/esp/ChunkedESP.kt

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import com.lambda.event.events.WorldEvent
2323
import com.lambda.event.events.onStaticRender
2424
import com.lambda.event.listener.SafeListener.Companion.listen
2525
import com.lambda.event.listener.SafeListener.Companion.listenConcurrently
26+
import com.lambda.module.Module
2627
import com.lambda.module.modules.client.StyleEditor
2728
import com.lambda.threading.awaitMainThread
2829
import com.lambda.util.world.FastVector
@@ -34,7 +35,7 @@ import java.util.concurrent.ConcurrentHashMap
3435
import java.util.concurrent.ConcurrentLinkedDeque
3536

3637
class ChunkedESP private constructor(
37-
owner: Any,
38+
owner: Module,
3839
private val update: ShapeBuilder.(World, FastVector) -> Unit
3940
) {
4041
private val rendererMap = ConcurrentHashMap<Long, EspChunk>()
@@ -53,10 +54,18 @@ class ChunkedESP private constructor(
5354

5455
init {
5556
//listen<WorldEvent.BlockUpdate.Client> { rebuildQueue.add(rendererMap[ChunkPos.toLong(it.pos)] ?: return@listen) }
56-
listen<WorldEvent.ChunkEvent.Load> { it.chunk.renderer.notifyChunks() }
57-
listen<WorldEvent.ChunkEvent.Unload> { rendererMap.remove(it.chunk.pos.toLong())?.notifyChunks() }
57+
listen<WorldEvent.ChunkEvent.Load> {
58+
if (!owner.isEnabled) return@listen
59+
it.chunk.renderer.notifyChunks()
60+
}
61+
62+
listen<WorldEvent.ChunkEvent.Unload> {
63+
if (!owner.isEnabled) return@listen
64+
rendererMap.remove(it.chunk.pos.toLong())?.notifyChunks()
65+
}
5866

5967
listenConcurrently<TickEvent.Post> {
68+
if (!owner.isEnabled) return@listenConcurrently
6069
if (++ticks % StyleEditor.updateFrequency == 0) {
6170
val polls = minOf(StyleEditor.rebuildsPerTick, rebuildQueue.size)
6271

@@ -67,18 +76,19 @@ class ChunkedESP private constructor(
6776
}
6877

6978
owner.onStaticRender {
70-
if (uploadQueue.isEmpty()) return@onStaticRender
71-
79+
if (!owner.isEnabled || uploadQueue.isEmpty()) return@onStaticRender
7280
val polls = minOf(StyleEditor.uploadsPerTick, uploadQueue.size)
73-
7481
repeat(polls) { uploadQueue.poll()?.invoke() }
7582
}
7683

77-
owner.listen<RenderEvent.Render> { rendererMap.values.forEach { it.renderer.render() } }
84+
owner.listen<RenderEvent.Render> {
85+
if (!owner.isEnabled) return@listen
86+
rendererMap.values.forEach { it.renderer.render() }
87+
}
7888
}
7989

8090
companion object {
81-
fun Any.newChunkedESP(
91+
fun Module.newChunkedESP(
8292
update: ShapeBuilder.(World, FastVector) -> Unit
8393
) = ChunkedESP(this@newChunkedESP, update)
8494
}
@@ -92,8 +102,8 @@ class ChunkedESP private constructor(
92102
.map { ChunkPos(chunk.pos.x + it.first, chunk.pos.z + it.second) }
93103

94104
fun notifyChunks() {
95-
neighbors.forEach {
96-
owner.rendererMap[it.toLong()]?.let {
105+
neighbors.forEach { chunkPos ->
106+
owner.rendererMap[chunkPos.toLong()]?.let {
97107
if (!owner.rebuildQueue.contains(it))
98108
owner.rebuildQueue.add(it)
99109
}

0 commit comments

Comments
 (0)