Subj : Re: JS_NewObject To : netscape.public.mozilla.jseng From : "Jochen Stier" Date : Mon Mar 29 2004 11:44 am Hi Brendan, With "internal class" I meant a C++ class for which I have created a corresponding JavaScript class using JS_DefineClass. Now I would like to instantiate an object of the "internal class" from within the C++ code and not the JavaScript. In the constructor of the internal class I call JS_NewObject to obtain a JSObject*. I can now use that JSObject* as one of the parameters to a JS_CallFunction and everything works fine. I can access the methods and members of the internal class via JavaScript. What I am confused about is the GC ? At what point does the GC delete the JSObject*, if at all ? I can certainly delete an object of the "internal class" but there must be some call to the Javascript engine to decrement the reference count to the JS_Object. OpenInventor has something like somevar->ref() and somevar->unref(). Am I correct to assume that JS_AddRoot and JS_RemoveRoot are equivalent to the the ref() and unref() calls of OpenInventor ? What about the JS_DeleteProperty that was mentioned by Sterling in the previous answer ? > You have to tell the engine when you no longer have an "internal class" > referring to the object returned from JS_NewObject. See JS_RemoveRoot, > which implies you have already called JS_AddNamedRoot passing the > address of a pointer to the JSObject. The pointer's storage must remain > live till you call JS_RemoveRoot, of course. I am confused about JS_AddRoot ? Do I have to call that after JS_NewObject or does that call happen automatically at least once when you call JS_NewObject ? It would assume that I only have to call JS_RemoveRoot in order to GC an object. Anyways, thanks for your help. I am starting to realize that I may be using the GC in a wrong way and that this may cause some of the problem I experience with the error reporting ..... Cheers Jochen "Brendan Eich" wrote in message news:40677929.2070108@meer.net... > Jochen Stier wrote: > > Hi, > > > > I am us JS_NewObject inside my C++ app to create stubs for internal classes. > > > What do you mean, exactly, by "internal class"? Can you show the code > that calls JS_NewObject and stores its return value? > > > > Is there a > > a way to delete the JSObject when I delete my internal classes. I am looking > > for something like JS_DeleteObject ? > > > There is no such API, nor should there be in a GC'd runtime's API. > > > > I know there is GC in JavaScript, but the interpretor can not know > > when I delete an internal class and the corresponding JS_Object is not > > needed anymore. > > > You have to tell the engine when you no longer have an "internal class" > referring to the object returned from JS_NewObject. See JS_RemoveRoot, > which implies you have already called JS_AddNamedRoot passing the > address of a pointer to the JSObject. The pointer's storage must remain > live till you call JS_RemoveRoot, of course. > > /be .