question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WebGLRenderer: Calling dispose() does not trigger dispose() on geometries, materials etc.

See original GitHub issue

Each time Pass.FullScreenQuad is used in a new WebGLRenderer, a dispose listener is attached to geometry. But when WebGLRenderer is disposed, geometry is not disposed, therefore the reference to the dispose listener is retained.

https://github.com/mrdoob/three.js/blob/b0a1e8ca7863aa399c31692fa5b76c40feca2485/examples/jsm/postprocessing/Pass.js#L37-L40

The listener holds a reference to info and bindingStates, which further refers to many other objects.

https://github.com/mrdoob/three.js/blob/4944e56c06ed284abe11b7654348fb1ea3ac04d4/src/renderers/webgl/WebGLGeometries.js#L5

image

image

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
gkjohnsoncommented, Nov 14, 2020

I guess one question is whether or not there’s another way to approach this that affords using two renderers at once or will it always be impossible to fully dispose of one renderer without incurring a reinitialization cost of shared geometry in the second.

One approach would be to enable disposing of objects directly on the renderer itself rather than with a dispose function:

renderer.disposeObject( geometry );

Or of course using a Map rather than WeakMap in WebGLGeometries. What is the benefit of using a WeakMap right now? It will let the geometry objects be garbage collected in JS but it will never allow for the gl bindings to be disposed of which will leave unreferenced VAOs in memory until the renderer is disposed of.

I guess it seems like there’s never a case where a user should feel comfortable with letting a BufferGeometry get garbage collected without calling dispose except for maybe just using a single renderer and having just disposed of the renderer. But even then they have to be sure that all of the geometry is left unreferenced or the renderer context can’t actually get cleaned up. If users are being told to call “dispose” on something it should actually properly dispose of the object. Are there strong feelings against using a Map rather than WeakMap in WebGLGeometries (and similar classes)?

Maybe WeakRef will make this all a bit simpler but we’ll see. In the mean time I can look into clarifying the wording on WebGLRenderer.dispose.

0reactions
Mugen87commented, Sep 15, 2020

dispose on the renderer and all geometry and materials in order to fully clean things up.

Yes, this is intended. You’ve already pointed out the reason (non-iterable WeakMaps) for why this is the case.

Perhaps those docs should be clarified if this is intended behavior?

Improving the docs is always good!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to dispose of objects – three.js docs
No, you have to explicitly dispose the geometry and material via dispose(). Keep in mind that geometries and materials can be shared among...
Read more >
Three.js: how to correctly dispose a scene in memory
Calling dispose on the materials, and geometries does not clear memory fully. Removing mesh from scene doesn't clear fully. And removing console ...
Read more >
The Big List of three.js Tips and Tricks!
Always try to reuse objects such as objects, materials, textures etc. (although updating some things may be slower than creating new ones, see...
Read more >
Three.js Cleanup
You free three.js resource this by calling the dispose function on textures, geometries, and materials. You could do this manually. At the start...
Read more >
Automatic disposal | React Three Fiber
dispose() , if present, on all unmounted objects. If you manage assets by yourself, globally or in a cache, this may not be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found