Garbage collection closure rules with Javascript -


I have a program that uses JavaScript as a scripting language for a large C ++ application. I use V8 and WebKit alternately, but the underlying sequence should not have any effect for this question.

In this application, I will get the dynamically created objects callback. I will make this callback compulsive ...

  function f () {var obj = CreateNewCallbackObj (); Obj.on Callback = Davork; // dowork is a function}   

Clearly there will be GC problems in it because obj is out of the scope and will eventually be removed

I want the object Should manage their lifetime. The object will eventually get a callback that will signal the end of his life, and when it does, he can destroy himself.

One thought obz. My own reference is by adding OBJ. It seems like a wrong way to do this, but unless the garbage collection algorithm is smart, it can work.

Is there any right way to do this? There is no built-in stable dome in it, which is designed to store objects, all JS objects are dynamically allocated, but some methods are needed to adhere around the JS engine.

Self-references not will guarantee that anything remains alive. In fact, it is not about the self-esteemed implementation of the GCD language (which I know) that uses alone reflection Do not even think of misusing GC algorithm or any other implementation-defined extension - this method is insanely is.

For the alternative: Create some always accessible (such as global) objects that hold all these objects (and thus keep them alive) and provide a method to remove any object. Then pray that no other reference has been found - or better, do not worry about it. The complete thing of GC is that when memory is released, then you should know / not take care.

You can also add a live property, check that at the beginning



Comments