Clean up Threejs WebGl contexts

kassette91 picture kassette91 · Feb 4, 2014 · Viewed 12.6k times · Source

I have a problem while cleaning up my WebGl-Scenes. I'm using Three.js with a WebGlRenderer. In my application I have to change the views quite often and therefore need to render new scenes all the time. Uptil now I destroy and reinitialize the entire Threejs scene. After switching the scenes about 15 - 20 times I get following warning:

WARNING: Too many active WebGL contexts. Oldest context will be lost.

After switching a couple of times more the context is lost completly and the application crashes.

Is there a way to destroy the current WebGl context, when cleaning up? Or does the WebGlRenderer always create a new WebGl context when being instantiated?

I'm using Three.js R64.

Answer

Konstantin Eletskiy picture Konstantin Eletskiy · Aug 17, 2015

I've got same problem, but i couldn't solve it using SPA, because of requirements.

There is .forceContextLoss() method in WebGLRenderer (rev 71, maybe was early) for this situations.

So, my code in 'deallocate' method is somemethig like

_self.renderer.forceContextLoss();
_self.renderer.context = null;
_self.renderer.domElement = null;
_self.renderer = null;