Skip to content

Commit

Permalink
BfBB: Fix memory leaks (#719)
Browse files Browse the repository at this point in the history
* BfBB: Fix memory leaks

* Fix models referencing destroyed textures
  • Loading branch information
seilweiss authored Nov 17, 2024
1 parent 0c2cc5e commit cd0461e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/HeavyIron/HIScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ export class HIScene implements SceneGfx {
texture.destroy(this.rw);
}

this.env.jsp.destroy(this.rw);

this.rw.destroy();
}

Expand Down Expand Up @@ -379,6 +381,10 @@ export class HIScene implements SceneGfx {

private loadTexture(asset: HIPAsset): boolean {
if (asset.data.byteLength === 0) return true;

// Don't load duplicate textures, temporary hack to prevent memory leaks
// TODO: Have each asset keep track of their own runtime data like in the OG game
if (this.textures.has(asset.id)) return true;

const stream = new RwStream(asset.data);
if (!stream.findChunk(RwPluginID.TEXDICTIONARY)) {
Expand All @@ -388,8 +394,14 @@ export class HIScene implements SceneGfx {

const texDict = RwTexDictionary.streamRead(stream, this.rw);
if (!texDict) return false;

// We only use the first texture
const texture = texDict.textures[0];
texDict.removeTexture(texture);

texDict.destroy(this.rw);

this.textures.set(asset.id, texDict.textures[0]);
this.textures.set(asset.id, texture);
return true;
}

Expand Down
6 changes: 6 additions & 0 deletions src/HeavyIron/JSP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,10 @@ export class JSP {
stream.pos = header.end;
}
}

public destroy(rw: RwEngine) {
for (const node of this.nodeList) {
node.atomic.destroy(rw);
}
}
}
13 changes: 13 additions & 0 deletions src/HeavyIron/rw/rwcore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,19 @@ export class RwTexDictionary {

return texDict;
}

public destroy(rw: RwEngine) {
for (const texture of this.textures) {
texture.destroy(rw);
}
}

public removeTexture(texture: RwTexture) {
const index = this.textures.indexOf(texture);
if (index !== -1) {
this.textures.splice(index);
}
}
}

export class RwFrame {
Expand Down

0 comments on commit cd0461e

Please sign in to comment.