MirrorTexture.dispose() never removes its imageProcessing observer (leaks the texture and everything it references)

Hi all! We found a memory leak in MirrorTexture: disposing a mirror doesn’t fully release it. In our game, every level change left the previous level in memory through its water reflection, about 4 MB each time, so long play sessions slowly grew.

In short: dispose() is supposed to unhook the mirror from the scene’s image-processing updates, but it tries to do so after the mirror has already forgotten its scene, so the unhook silently never happens. The disposed mirror stays reachable, and so does everything it points at (its render list, its predicate closure and the meshes it last rendered).

The fix is a small reordering inside dispose(); details, numbers and a suggested patch are below.

Repro (Playground): Babylon.js Playground

Each “zone” is a reflective floor (a MirrorTexture) plus 40 spheres. The buttons dispose the zone and build a new one, the way a game changes levels. The on-screen counters show the imageProcessing observer count and how many disposed mirrors are still hooked up. A “Workaround” toggle removes the observer before dispose() for comparison.

Measured (Chrome, Babylon 9.31.0, JS heap after forced GC; disposed mirrors tracked with WeakRef):

Zones disposed 0 10 20 50
dispose() as is: heap 20.6 MB 25.5 MB 28.7 MB 39.2 MB
dispose() as is: disposed mirrors still alive 0 10 20 50
observer removed first: heap 20.5 MB 22.2 MB 22.7 MB 22.2 MB
observer removed first: disposed mirrors still alive 0 1 1 1

(Screenshots and a chart attached.)

Where: mirrorTexture.pure.ts, unchanged on master:

public override dispose() {
    super.dispose();                    // BaseTexture.dispose() sets this._scene = null
    const scene = this.getScene();      // so this is now null...

    if (scene) {                        // ...and the observer is never removed
        scene.imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingConfigChangeObserver);
    }
    this._sceneUBO?.dispose();
}

Even smaller repro (console only):

const scene = new BABYLON.Scene(engine);
const before = scene.imageProcessingConfiguration.onUpdateParameters.observers.length;
for (let i = 0; i < 5; i++) new BABYLON.MirrorTexture("m" + i, 256, scene, true).dispose();
setTimeout(() => console.log(before, scene.imageProcessingConfiguration.onUpdateParameters.observers.length), 0); // 0 → 5

Real-world impact: in our game each zone has a water mirror whose renderListPredicate closes over the zone’s state, so every zone the player left stayed in memory: meshes, materials and textures, about 4 MB per zone round trip. We found it with heap snapshots. The retainer path was scene._imageProcessingConfiguration.onUpdateParameters._observers[i].callback → the MirrorTexture → _objectRenderer.renderListPredicate / _activeMeshes → the old zone.

Suggested fix: remove the observer before calling super.dispose():

public override dispose() {
    const scene = this.getScene();
    scene?.imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingConfigChangeObserver);
    this._imageProcessingConfigChangeObserver = null;
    super.dispose();
    this._sceneUBO?.dispose();
}

Our workaround in the meantime: remove mirror._imageProcessingConfigChangeObserver from scene.imageProcessingConfiguration.onUpdateParameters ourselves, then call dispose().

Happy to open a PR with this fix and a unit test if that helps.

I’m going to look into this and validate it because if true it will give me huge gains in my main project. But just a general read over this is legit.

Thanks for the detailed report and repro! We confirmed the issue: super.dispose() clears the scene reference before the observer can be removed. Your proposed reordering fixes it.

Would you like to open a PR with the fix and a regression test? If not, no worries—we’ll take care of it.

Thanks for checking @Evgeni_Popov! I’m happy to take care of the PR, I’ll get started on it right now. Cheers.

The PR is up: Fix MirrorTexture.dispose leaking its imageProcessing observer by christabone · Pull Request #19035 · BabylonJS/Babylon.js · GitHub

It moves the observer removal before super.dispose() and adds a NullEngine regression test that creates and disposes a few MirrorTextures and checks the onUpdateParameters observer count returns to baseline (it fails with the old order). I also checked the other dispose() overrides for the same pattern and didn’t find another affected texture.

@Pryme8, hope it helps with your project too!

I was noticing a huge memory issues with my rear view mirrors. I’m hoping this is the magic bullet.