Fix WebGL memory leak from undisposed intermediate geometries in GLTF loading #791
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
I'm submitting this fix to address a critical WebGL memory leak observed during GLTF geometry loading in the event display.
Currently, whenever a GLTF geometry is loaded (for example when switching between detector views or reloading the same detector), the import logic clones and transforms multiple geometries before merging them. While the merged geometry is rendered correctly, the intermediate cloned geometries are never disposed, leading to unbounded GPU memory growth over time.
The Issue
The root cause is that intermediate
BufferGeometryobjects created during GLTF loading are never explicitly disposed.The behaviour:
Each call to
loadGLTFGeometry()clones multiple geometries usinggeometry.clone().applyMatrix4()and stores them temporarily for merging.The result:
mergeGeometries()creates a new merged geometry, but the original cloned geometries remain allocated in WebGL memory.Why it matters:
WebGL buffers are not freed by JavaScript garbage collection. Repeated geometry loads progressively increase GPU memory usage, eventually causing WebGL context loss, browser crashes, or severe performance degradation during long sessions.
The Fix
I updated the GLTF import logic to explicitly dispose all intermediate cloned geometries immediately after they are merged.
After
BufferGeometryUtils.mergeGeometries()completes and the merged mesh is added to the scene, all temporaryBufferGeometryinstances used as merge inputs are now cleaned up usingdispose().This ensures that GPU memory is released correctly while keeping rendering behavior unchanged.
Verification
You can verify this fix using browser DevTools: