I'm not able to dispose scene

Hello. I’ve been trying to solve this for days.

I have some scenes that I want to change when necessary.
Switching from the 1st to the 2nd one, it works fine. I used scene.dispose(), then using the same engine and canvas I call/create the next one.

However, from the 2nd to the 3rd one, it doesn’t dispose. I changed my code a lot of times (React), but it always ends up showing the same error. It’s only when I try to dispose this second scene, the others work.

The error:

TypeError: Cannot read property ‘uniqueId’ of undefined

Scene.removeGeometry

  2862 |     const lastGeometry = this.geometries[this.geometries.length - 1];
  2863 |     this.geometries[index] = lastGeometry;
  2864 |     if (this.geometriesByUniqueId) {
> 2865 |         this.geometriesByUniqueId[lastGeometry.uniqueId] = index;
  2866 | ^       this.geometriesByUniqueId[geometry.uniqueId] = undefined;
  2867 |     }

Geometry.dispose

  914 |       this._boundingInfo = null;
  915 |
> 916 |       this._scene.removeGeometry(this);
  917 |  ^    this._isDisposed = true;
  918 |      }
  919 | 

(anonymous function)

  381 |    this.materials = [];
  382 |
  383 |       this.geometries.forEach((o) => {
> 384 |         o.dispose();
  385 | ^     });
  386 |    this.geometries = [];
  387 | 

AssetContainer.dispose

  380 |      });
  381 |      this.materials = [];
  382 |
> 383 |      this.geometries.forEach((o) => { 
  384 | ^         o.dispose();
  385 |      });
  386 |      this.geometries = [];

Observer.callback

  65 |
  66 |        scene.onDisposeObservable.add(() => { 
  67 |            if (!this._wasAddedToScene) {
> 68 |                this.dispose();    
  69 | ^           }
  70 |        });
  71 |    }

Observable.notifyObservers

  326 |      if (obs.scope) {
  327 |                    state.lastReturnValue = obs.callback.apply(obs.scope, [eventData, state]);
  328 |                } else {
> 329 |                    state.lastReturnValue = obs.callback(eventData, state);
  330 | ^              }
  331 |
  332 |                if (obs.unregisterOnNextCall) {

Scene.dispose

  4140 |        }
  4141 | 
  4142 |        // Events
> 4143 |        this.onDisposeObservable.notifyObservers(this);
  4144 | ^ 
  4145 |        this.onDisposeObservable.clear();
  4146 |        this.onBeforeRenderObservable.clear();

(anonymous function)

  58 |
> 59 |      scene.dispose() 
  60 | ^

I don’t know how to debug it. I tried many different things already.
I’m pretty sure there is something on this scene. Looks like something related to geometries, I don’t know.

In order to load them I’m calling an external .gltf using SceneLoader like this:

SceneLoader.Append("url/Monster/", 'Monster.gltf', scene)

Thanks.

Hello and welcome!
we need to see a repro in the playground to help

Thank you for answering.
I signed a contract of confidentiality, so unfortunately I think I’m not allowed to divulge it.
For now, can I provide any other kind of information that would help you?

It sounds like this problem is fairly generic. Can you create a repro with the playground without exposing any of the internal work?

1 Like