# Mesh.dispose trigger Uncaught TypeError: Cannot read property 'meshes' of null

**URL:** <https://forum.babylonjs.com/t/mesh-dispose-trigger-uncaught-typeerror-cannot-read-property-meshes-of-null/22301>\
**Category:** Bugs\
**Created:** [July 11, 2021, 12:37pm UTC](https://forum.babylonjs.com/t/mesh-dispose-trigger-uncaught-typeerror-cannot-read-property-meshes-of-null/22301 "2021-07-11T12:37:11Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Seyyed\_Muhammad\_Amir](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/seyyed_muhammad_amir/32/10594_2.png) [@Seyyed\_Muhammad\_Amir](https://forum.babylonjs.com/u/Seyyed_Muhammad_Amir)\
**Post date:** [July 11, 2021, 12:37pm UTC](https://forum.babylonjs.com/t/mesh-dispose-trigger-uncaught-typeerror-cannot-read-property-meshes-of-null/22301/1 "2021-07-11T12:37:11Z")

</div>

Hello,  
When I dispose meshes with `mesh.dispose();`  
disposing meshes give me error:

```auto
Uncaught TypeError: Cannot read property 'meshes' of null
    at Array.e.splice (babylonjs.js:16)
    at Array.e.splice (babylonjs.js:16)
    at Array.e.splice (babylonjs.js:16)
    at Array.e.splice (babylonjs.js:16)
    at Array.e.splice (babylonjs.js:16)
    at Array.e.splice (babylonjs.js:16)
    at Array.e.splice (babylonjs.js:16)
    at Array.e.splice (babylonjs.js:16)
    at Array.e.splice (babylonjs.js:16)
    at Array.e.splice (babylonjs.js:16)

```

Here is my Code

```
    mesh.onDispose = function(){
        mesh = null;
    };
    mesh.dispose();

```

I unminified babylon.js and the issue is from here:

```auto
return (
    0 === e.length &&
        t.getScene().meshes.forEach(function (e) {
            e._markSubMeshesAsLightDirty();
         }),
      o);

```

---

<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:** [July 12, 2021, 9:06am UTC](https://forum.babylonjs.com/t/mesh-dispose-trigger-uncaught-typeerror-cannot-read-property-meshes-of-null/22301/3 "2021-07-12T09:06:06Z")

</div>

It seems the scene is already disposed when you dispose the mesh, but without a repro in the Playground it’s going to be hard to say more.

---

<div class="post-metadata">

**Author:** ![Seyyed\_Muhammad\_Amir](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/seyyed_muhammad_amir/32/10594_2.png) [@Seyyed\_Muhammad\_Amir](https://forum.babylonjs.com/u/Seyyed_Muhammad_Amir)\
**Post date:** [July 13, 2021, 5:42am UTC](https://forum.babylonjs.com/t/mesh-dispose-trigger-uncaught-typeerror-cannot-read-property-meshes-of-null/22301/4 "2021-07-13T05:42:13Z")

</div>

It happen when only there is a mesh remains in scene. If more than one mesh exist, error not triggered.  
I don’t have any ` scene.dispose` or `engine.dispose` in my code, but it was working till a few weeks ago, I don’t know what happened? I did a few steps to optimize me scene from [this page](https://doc.babylonjs.com/divingDeeper/scene/optimize_your_scene)  
It is a huge project, but your help let me think checking where scene customized. I will try to make it a Playground. Thanks @Evgeni_Popov

---

<div class="post-metadata">

**Author:** ![Seyyed\_Muhammad\_Amir](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/seyyed_muhammad_amir/32/10594_2.png) [@Seyyed\_Muhammad\_Amir](https://forum.babylonjs.com/u/Seyyed_Muhammad_Amir)\
**Post date:** [July 14, 2021, 7:19am UTC](https://forum.babylonjs.com/t/mesh-dispose-trigger-uncaught-typeerror-cannot-read-property-meshes-of-null/22301/5 "2021-07-14T07:19:23Z")

</div>

I found the problem, but it is so weird!!!  
One of my optimizations was reduce `mapSize` of `shadowGenerator` by FPS after loading each new mesh and this is the code:

```auto
        var FPS = engine.getFps().toFixed();
        shadowGenerator.mapSize = Math.min(512, parseInt(512 * (FPS / 60))) - parseInt(isSmallDevice() ? 64 : 0);

```

When I comment it no more errors found.  
Can anyone give me another solution to reduce `mapSize` on lower power devices?

---

<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:** [July 14, 2021, 9:23am UTC](https://forum.babylonjs.com/t/mesh-dispose-trigger-uncaught-typeerror-cannot-read-property-meshes-of-null/22301/6 "2021-07-14T09:23:01Z")

</div>

Are you doing this each frame? You should not, it recreates a number of textures under the hood, and the system may not have time to render a new frame that you already change for a new size. Also, you should not change the size if the difference in fps is not significant enough from the last time you changed the size. There’s no point to change from 503 to 494 for eg (if fps changes from 59 to 58).

---

<div class="post-metadata">

**Author:** ![Seyyed\_Muhammad\_Amir](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/seyyed_muhammad_amir/32/10594_2.png) [@Seyyed\_Muhammad\_Amir](https://forum.babylonjs.com/u/Seyyed_Muhammad_Amir)\
**Post date:** [July 14, 2021, 11:22am UTC](https://forum.babylonjs.com/t/mesh-dispose-trigger-uncaught-typeerror-cannot-read-property-meshes-of-null/22301/7 "2021-07-14T11:22:28Z")

</div>

New explore: the problem is from:  
` var FPS = engine.getFps().toFixed();`  
not the `mapSize`

@Evgeni_Popov

> Are you doing this each frame?

No, After user adds a new mesh.

I played around with `mapSize` of `shadowGenerator`, it has huge effect on cpu usage.  
**My point is to reduce `mapSize` based on user’s device** , doesn’t matter when and where for me, I just want higher quality for better devices, and higher fps for lower devices.  
Thanks for considering the issue

---

<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:** [July 14, 2021, 3:29pm UTC](https://forum.babylonjs.com/t/mesh-dispose-trigger-uncaught-typeerror-cannot-read-property-meshes-of-null/22301/8 "2021-07-14T15:29:22Z")

</div>

I guess you will need to provide a repro in the Playground, I can’t understand why `engine.getFps().toFixed()` could be a problem.

---

<div class="post-metadata">

**Author:** ![Seyyed\_Muhammad\_Amir](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/seyyed_muhammad_amir/32/10594_2.png) [@Seyyed\_Muhammad\_Amir](https://forum.babylonjs.com/u/Seyyed_Muhammad_Amir)\
**Post date:** [July 14, 2021, 3:31pm UTC](https://forum.babylonjs.com/t/mesh-dispose-trigger-uncaught-typeerror-cannot-read-property-meshes-of-null/22301/9 "2021-07-14T15:31:22Z")

</div>

Ok, I will try to make a Playground  
@Evgeni_Popov

---

<div class="post-metadata">

**Author:** ![Seyyed\_Muhammad\_Amir](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/seyyed_muhammad_amir/32/10594_2.png) [@Seyyed\_Muhammad\_Amir](https://forum.babylonjs.com/u/Seyyed_Muhammad_Amir)\
**Post date:** [July 17, 2021, 6:11am UTC](https://forum.babylonjs.com/t/mesh-dispose-trigger-uncaught-typeerror-cannot-read-property-meshes-of-null/22301/10 "2021-07-17T06:11:46Z")

</div>

At last I made it,  
It happen when a mesh added and mesh accepts shadow, and after that change `mapSize` of `shadowGenerator` then remove mesh.  
And I successfully made [playground](https://playground.babylonjs.com/#CA7U0Y#16) (check console)  
**Note** : If another mesh exist in scene that accepts shadow, error will not shows up.

---

<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:** [July 17, 2021, 10:48am UTC](https://forum.babylonjs.com/t/mesh-dispose-trigger-uncaught-typeerror-cannot-read-property-meshes-of-null/22301/11 "2021-07-17T10:48:59Z")

</div>

Thanks for the repro, here’s the PR that will fix the problem:

> <https://github.com/BabylonJS/Babylon.js/pull/10683>
>
> See https://forum.babylonjs.com/t/mesh-dispose-trigger-uncaught-typeerror-cannot…-read-property-meshes-of-null/22301/9
> 
> Simple repro: https://playground.babylonjs.com/#VRUFYH#4

---

<div class="post-metadata">

**Author:** ![Seyyed\_Muhammad\_Amir](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/seyyed_muhammad_amir/32/10594_2.png) [@Seyyed\_Muhammad\_Amir](https://forum.babylonjs.com/u/Seyyed_Muhammad_Amir)\
**Post date:** [July 17, 2021, 6:10pm UTC](https://forum.babylonjs.com/t/mesh-dispose-trigger-uncaught-typeerror-cannot-read-property-meshes-of-null/22301/12 "2021-07-17T18:10:43Z")

</div>

Thanks @Evgeni_Popov , I’m not using **.ts** and so I did my own changes on **babaylonjs.js** file like so:  
In case someone need:

```auto
(e.prototype.recreateShadowMap = function () {
                            var e = this._shadowMap;
                            if (e) {
                                var t = e.renderList;
                                this._disposeRTTandPostProcesses(), this._initializeGenerator(), (this.filter = this.filter), this._applyFilterValues();
                                //(this._shadowMap.renderList = t);
                                 if (t) {
                                    // Note: don't do this._shadowMap!.renderList = renderList;
                                    // The renderList hooked array is accessing the old RenderTargetTexture (see RenderTargetTexture._hookArray), which is disposed at this point (by the call to _disposeRTTandPostProcesses)
                                    if (!this._shadowMap.renderList) {
                                        this._shadowMap.renderList = [];
                                    }
                                    for (const mesh of t) {
                                        this._shadowMap.renderList.push(mesh);
                                    }
                                } else {
                                    this._shadowMap.renderList = null;
                                }
                            }
                        })

```
