My scene objects are structured around a single root Object3D, with data loaded as a tree of Object3Ds branching from this root. Meshes are attached to the leaf Object3Ds using BufferGeometry/MeshPhongMaterial. To clear the existing tree structure, I use the following method:
clearScene:
function (obj) {
if (obj instanceof THREE.Mesh)
{
obj.geometry.dispose();
obj.geometry = undefined;
obj.material.dispose();
obj.material = undefined;
obj = undefined;
}
else
{
if (obj.children !== undefined) {
while (obj.children.length > 0) {
this.clearScene(obj.children[0]); // removing children changes the length of the array.
obj.remove(obj.children[0]);
}
}
}
}
Let's consider a simple tree structure:
- Scene (Scene)
- Root (Object3D)
- Branch (Object3D)
- Leaf (Mesh)
- Branch (Object3D)
- Root (Object3D)
After adding this structure to the scene, observation of the heap in Chrome's dev tools reveals 3 Object3Ds and 2 Mesh objects (apart from prototypes).
Upon calling clearScene(Root), the traversal through the tree removes Object3Ds and cleans up meshes. However, inspection of the heap shows that although the Object3Ds have been removed, the 2 Mesh objects (along with their associated BufferGeometry and Material objects) persist. Reloading the data after clearing results in 3 Object3Ds (as expected), but 4 Meshes (not intended).
This indicates a lingering reference issue, despite no retainers being found in the heap.
It seems like there is something else at play causing these objects to remain active.
r69dev (I was seeing the same in r68), testing in Chrome 36.0.1985.125