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

**URL:** <https://forum.babylonjs.com/t/mirrortexture-dispose-never-removes-its-imageprocessing-observer-leaks-the-texture-and-everything-it-references/64370>\
**Category:** Bugs\
**Created:** [October 22, 2026, 12:22am UTC](https://forum.babylonjs.com/t/mirrortexture-dispose-never-removes-its-imageprocessing-observer-leaks-the-texture-and-everything-it-references/64370 "2026-10-22T00:22:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ctabone](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/ctabone/32/52182_2.png) [@ctabone](https://forum.babylonjs.com/u/ctabone)\
**Post date:** [October 22, 2026, 12:22am UTC](https://forum.babylonjs.com/t/mirrortexture-dispose-never-removes-its-imageprocessing-observer-leaks-the-texture-and-everything-it-references/64370/1 "2026-10-22T00:22:47Z")

</div>

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](https://playground.babylonjs.com/#F4H3SJ)

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:

```ts
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):

```js
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()`:

```ts
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.

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/a/f/afdcdc5bf52d249b263da0cd9a338b647930d581.jpeg)

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/f/c/fced5b3753594a7e617748cdc4abf39b403bba4e.png)

---

<div class="post-metadata">

**Author:** ![Pryme8](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/pryme8/32/7212_2.png) [@Pryme8](https://forum.babylonjs.com/u/Pryme8)\
**Post date:** [October 22, 2026, 3:47am UTC](https://forum.babylonjs.com/t/mirrortexture-dispose-never-removes-its-imageprocessing-observer-leaks-the-texture-and-everything-it-references/64370/2 "2026-10-22T03:47:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [October 22, 2026, 1:12pm UTC](https://forum.babylonjs.com/t/mirrortexture-dispose-never-removes-its-imageprocessing-observer-leaks-the-texture-and-everything-it-references/64370/3 "2026-10-22T13:12:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ctabone](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/ctabone/32/52182_2.png) [@ctabone](https://forum.babylonjs.com/u/ctabone)\
**Post date:** [October 22, 2026, 1:13pm UTC](https://forum.babylonjs.com/t/mirrortexture-dispose-never-removes-its-imageprocessing-observer-leaks-the-texture-and-everything-it-references/64370/4 "2026-10-22T13:13:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ctabone](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/ctabone/32/52182_2.png) [@ctabone](https://forum.babylonjs.com/u/ctabone)\
**Post date:** [October 22, 2026, 1:30pm UTC](https://forum.babylonjs.com/t/mirrortexture-dispose-never-removes-its-imageprocessing-observer-leaks-the-texture-and-everything-it-references/64370/5 "2026-10-22T13:30:01Z")

</div>

The PR is up: [Fix MirrorTexture.dispose leaking its imageProcessing observer by christabone · Pull Request #19035 · BabylonJS/Babylon.js · GitHub](https://github.com/BabylonJS/Babylon.js/pull/19035)

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!

---

<div class="post-metadata">

**Author:** ![Pryme8](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/pryme8/32/7212_2.png) [@Pryme8](https://forum.babylonjs.com/u/Pryme8)\
**Post date:** [October 22, 2026, 4:23pm UTC](https://forum.babylonjs.com/t/mirrortexture-dispose-never-removes-its-imageprocessing-observer-leaks-the-texture-and-everything-it-references/64370/6 "2026-10-22T16:23:37Z")

</div>

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