I have a program that uses Javascript as a scripting language binding to a larger C++ application. I use V8 and webkit interchangeably, but the underlying runtime shouldn t matter for this question.
In this application, I have dynamically created objects will receive callbacks. I ll create this callback binding like this...
function f() { var obj = CreateNewCallbackObj(); obj.onCallback = dowork; // dowork is a function }
Clearly this will have GC problems because obj has gone out of scope and will be deleted eventually.
What I want is for the object to self manage its lifetime. The object will eventually receive a callback that will signal the end of its life, and when that happens it can delete itself.
One thought is to self-reference by adding an obj.myself=obj. This seems like the wrong way to do it, but it might work unless a garbage collection algorithm is smart.
Is there a right way to do this? There is no underlying persistant DOM that is built to store objects in, all of the JS objects are allocated dynamically as needed but need some way to stick around within the JS engine.