Destroyed primitives leaked

Setup steps

  • Add a primitive to the scene via scene.primitives.add(primitive)
  • Remove to primitive from the scene vis scene.primitives.remove(primitive)

The primitive is correctly destroyed, however the draw commands it has created are still referenced via the frustumCommandList (of the scene / view). This can be observed by memory profiling the browser – comparing snapshots.

Analysis
The problem is that “old” draw commands are not cleared when rebuilding the frustumCommands in View. It seems that createPotentiallyVisibleSet() correctly clears the indices used for keeping track of the updated commands by setting the indices to 0 – see the following snippet:

const frustumCommandsList = this.frustumCommandsList;
const numberOfFrustums = frustumCommandsList.length;
const numberOfPasses = Pass.NUMBER_OF_PASSES;
for (let n = 0; n < numberOfFrustums; ++n) {
  for (let p = 0; p < numberOfPasses; ++p) {
    frustumCommandsList[n].indices[p] = 0;
  }
}

However the commands array referencing the actual draw commands are not cleared, causing the leak if fewer draw commands appear in the subsequent frustum.

Possible fix
I guess this can be fixed be clearing the command lists along with the indices in the above loop:

    frustumCommandsList[n].indices[p] = 0;
    frustumCommandsList[n].commands[p].length = 0;