virtualnobi
virtualnobi

Reputation: 1180

Cleaning up before an object is garbage-collected in javascript - how to?

I'd like to clean up some dependent data (in this case, remove webgl textures from the GPU) when a Javascript object (in this case, representing a shape to be drawn) goes out of scope - just before it is garbage-collected. So I was looking for something like finalize() or destroy (), which I can use to remove the webgl texture when the Javascript object looses its last reference, before it is garbage-collected.

I've looked through the web and stackoverflow, and nearly come to the point of believing that Javascript does not have this feature, but I can't believe that... How to achieve this effect in the absence of finalize et.al.?

Upvotes: 1

Views: 461

Answers (2)

Ian Mackenzie
Ian Mackenzie

Reputation: 55

I realize this is six years late, but this questions still comes up on Google searches - the WebGL spec does seem to say that buffers and textures will be automatically destroyed on the graphics card when the corresponding WebGLBuffer or WebGLTexture is destroyed. See for example the documentation for deleteBuffer:

If buffer was generated by a different WebGLRenderingContext than this one, generates an INVALID_OPERATION error.

Mark for deletion the buffer object contained in the passed WebGLBuffer, as if by calling glDeleteBuffers. If the object has already been marked for deletion, the call has no effect. Note that underlying GL object will be automatically marked for deletion when the JS object is destroyed, however this method allows authors to mark an object for deletion early.

Upvotes: 1

virtualnobi
virtualnobi

Reputation: 1180

So there's no way to know when the Javascript GC will destroy an object.

(This answer only to close the question - does a better way exist?)

Upvotes: 0

Related Questions