From 646806d132b4bfec75e7a8fee584a007e8eae86d Mon Sep 17 00:00:00 2001 From: Evan Date: Wed, 12 Feb 2025 12:35:10 -0800 Subject: [PATCH] remove old units in GameView to prevent memory leak --- src/core/game/GameView.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/core/game/GameView.ts b/src/core/game/GameView.ts index e79922c7c..c62c4b5ad 100644 --- a/src/core/game/GameView.ts +++ b/src/core/game/GameView.ts @@ -223,6 +223,8 @@ export class GameView implements GameMap { private defensePostGrid: DefenseGrid; + private toDelete = new Set(); + constructor( public worker: WorkerClient, private _config: Config, @@ -244,6 +246,9 @@ export class GameView implements GameMap { } public update(gu: GameUpdateViewData) { + this.toDelete.forEach((id) => this._units.delete(id)); + this.toDelete.clear(); + this.lastUpdate = gu; this.updatedTiles = []; @@ -283,6 +288,10 @@ export class GameView implements GameMap { this.defensePostGrid.removeDefense(unit); } } + if (!unit.isActive()) { + // Wait until next tick to delete the unit. + this.toDelete.add(unit.id()); + } }); }