Why does WebGLRenderTarget.setSize call this.dispose()?

I am trying to understand when and how to dispose of the objects.

While looking at documentation, samples and source code, the first use of dispose that I found is the one in WebGLRenderTarget.setSize:

	setSize( width, height, depth = 1 ) {

		if ( this.width !== width || this.height !== height || this.depth !== depth ) {

			this.width = width;
			this.height = height;
			this.depth = depth;

			this.texture.image.width = width;
			this.texture.image.height = height;
			this.texture.image.depth = depth;



		this.viewport.set( 0, 0, width, height );
		this.scissor.set( 0, 0, width, height );


What is this call to dispose for?

The instance it is trying to dispose is not at the end of its life… unless I am missing something.

1 Like

An instance of WebGLRenderTarget is basically a user-friendly representation of a framebuffer.

Framebuffers can’t be resized. If you call setSize(), the internal framebuffer has to be deleted and a new one allocated.


Ok, but why this?

Then it should be something like oldInternalFrameBuffer.dispose(), not this.dispose().

Isn’t calling this.dispose() going to break this?

No that does not happen. dispose() only dispatches an event. An internal event listener then performs the clean up of the mentioned WebGL resources.

1 Like

Ok, so when in doubt, better to call dispose() than risking a memory leak?

I’m saying this because I would never have thought about calling dispose in this case.

The documentation says if a class has dispose, then it should be called when the objects become unused, not when… the value of some properties changes and some internal buffer changes are triggered.

Is it enough for me to stick with calling dispose when objects are unused, like the documentation says?

Or are there better guidelines on when to call dispose?


Well, you don’t have to call it. setSize() does so you don’t have to worry about it.

1 Like