3. Context. Why do you need to do this? We might know a better way to accomplish your goal.
We need to clear everything in the cesium viewer and add new entities. When we clear the viewer, we want to make sure that there is nothing left from the previous model.
4. The Cesium version you’re using, your operating system and browser.
The actual problem isn’t adding the new entities, but it’s destroying primitives twice.
Entities are built on top of primitives (you can read a bit about using the lower level API here). When you create an entity, it also creates a primitive for it. You can confirm this by running:
After your create the first blue box. Both return 1. When you call removeAll() on the entities, it takes care of destroying its own primitive. Then when you try to remove the primitive with the removeAll() it will attempt to destroy that same primitive. The Entity and Primitive layers weren’t really designed to be used together that way. Another issue can be seen in this sandcastle, where I add a primitive, an entity, and then try to remove all primitives. This crashes because the primitive that belongs to the entity gets destroyed without communicating to the Entity layer.
A workaround to this would be to use removeAll() on the entities, and separately keep track of primitives created and remove them individually with viewer.scene.primitives.remove(primitive).
Sorry, I spoke too soon. I am still confused by the relationship between entities and primitives. See this sandcastle.
Considering all this, is there a better way to just clear everything from the viewer? I can of course instantiate another viewer, but that will have some other overheads.
It looks like Cesium re-uses these primitives in the lower levels. After the last step, when a single primitive remains, if you add another entity, there will still be just 1 primitive.
I would not worry about these internals of Cesium. The general advice is to not mix the Entity and Primitive layers in the same application, but if you are doing it then relying on removeAll for entities and manually removing primitives you added directly should be correct. If doing this you do find that there is a memory leak then that would be a bug and you can open an issue on github for that that someone could take a look at and fix.
Looks like I posted too soon as well! There’s no reason using both the Entity and Primitive layers together shouldn’t work. The issues we’re running into here is just when we try to remove primitives we didn’t explicitly add (so let the Entity system handle its creation & cleaning up of its own primitives)
are you saying, that mixing entities and primitives is fine
Yes!
that if the entity api adds a primitive under the hood, then the entity api will clean it up when we call “removeAll()”
Yes!
but if we also add our own primitives, then we need to do the clean up, and we should not use “removeAll()”
Not exactly. You can still call “removeAll()”, but it will only handle cleaning up of the primitives that the entity system created. If you added your own primitives, A and B, you should keep track of them and remove them by calling the primitives.remove: