Three.JS locally scoped loader not being destroyed at end of function scope?

65 views
Skip to first unread message

vaugham hong

unread,
May 2, 2015, 12:58:52 AM5/2/15
to v8-u...@googlegroups.com
Hi,

I'm a little confused on a scoping issue and was hoping someone could enlighten me or point me in the right direction. I'm looking at the three.js library and I see the following on line 19-27 (https://github.com/mrdoob/three.js/blob/master/examples/js/loaders/OBJLoader.js).

load: function ( url, onLoad, onProgress, onError ) {
var scope = this;
var loader = new THREE.XHRLoader( scope.manager );
loader.setCrossOrigin( this.crossOrigin );
loader.load( url, function ( text ) {
onLoad( scope.parse( text ) );
}, onProgress, onError ); 
},

From what I know, it looks like loader is a locally scoped to the load function. The inner callback is only a closure on the function onLoad and scope - no references to loader. This means at the end of the load function, loader should be out of scope and get cleaned up. However this does not happen, and the callback is called when the loader completes.

I duplicated the code in v8 and ran it with standard GC (IdleNotification) and aggressive GC (LowMemoryNotification). With the standard GC, the loader is not destroyed (right away) and the callback is called. Running in aggressive the loader is destroyed at the end of the function immediately <== this is the part that confuses me. Three.JS can't be doing this wrong can they? Am I missing something with regards to closures that I'm not aware of?

Any insights or pointing me in the right direction would be greatly appreciated. Thanks.

Vom

Andreas Rossberg

unread,
May 4, 2015, 7:22:22 AM5/4/15
to v8-u...@googlegroups.com
JavaScript is a truly higher-order language. Scope and life time are entirely separate concepts in such a language. The end of a scope has no direct implication on the life time of objects bound to variables in that scope. Only garbage collection can figure out whether an object is dead, and run its finalisers, if any (which, I suppose, is what you mean by "destruction"?). Note that there explicitly is _no guarantee_ when finalisers are being run (or if ever).

/Andreas


--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups "v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages