Cannot destroy models in Cesium 1.97

I’ve been creating models as primitives in Cesium 1.97 so I can use CustomShaders, MatrixModels, and so on. When I wanted to get rid of a model previously, I would use .destroy() on the model. Now I find that this isn’t working, and it throws a variety of errors depending on the context.

Here is a basic Sandcastle showing the problem. It loads a model from GLTF, flies to it, waits a few seconds, then tries to eliminate it. In this case, I get the error “DeveloperError: This object was destroyed, i.e., destroy() was called.” when I call destroy(). (And it doesn’t get rid of the model.)

On my own code (which is considerably more complicated), I get a different error (“TypeError: Cannot read properties of undefined (reading ‘_target’)”), citing line 710, position 26931 of the 1.97 build file, which seems to be related to it trying to set uniforms to something that is destroyed (this happens even if I am not using CustomShaders).

Either way, I can’t destroy/remove models anymore, which is a problem, and I’m totally stumped.

OK, digging around a bit more. With my Sandcastle up there, I can remove it if I change the removal line to to

  viewer.scene.primitives.remove(plane);

Which is something. My code still throws the TypeError. :-/ It does remove it from the PrimitiveCollection, but somehow still causes a total crash. …

OK, I think I (finally) solved my problem. The issue was that in the function that detected whether the model should be removed, it was also performing a matrix transformation on it. Apparently there is a delay between telling the transformation to occur and it being executed, and it was removing it from the collection, and then trying to perform the transformation. So I refactored it so it would delete before the transformation, and that did the trick… ugh.

Hi @Nucleon,

Yes, using scene.primitives.remove() is the recommended way to remove a model from the scene. PrimitiveCollection.remove() does call destroy() on the primitive that is removed to clean up its resources. model.destroy() is rarely needed in user code.

Yes, some properties of Model only set a dirty flag and are updated in the next update() loop, this delay helps avoid repetitive calculations in cases where the user sets multiple properties of the model in a single frame.

If you need to handle race conditions in your code, you can check the boolean value of model.isDestroyed() and skip updating the matrix in that case.

1 Like