Subj : Re: custom object lifetime question. To : Chris Tchou From : Brendan Eich Date : Wed Jul 21 2004 05:34 pm Chris Tchou wrote: > "Brendan Eich" wrote in message > news:40FD3626.1010804@meer.net... > >>donteventthinkaboutit@sbcglobal.net wrote: >>If you use JS_DefineObject, you are (a) creating a new object; (b) >>binding it to another object by defining a property in that other object >>whose value is a reference to the new object created in (a). If you no >>longer need the object created in (a), you need *at the least* to make >>the object in which you defined it as a property value become garbage, >>or else delete the property defined in (b). > > > What if an object you created, "A" has a dependence on another object "B" What *exactly* do you mean by "has a dependence"? > that isn't reflected in the properties of A (i.e. no property of A is a > reference to B)? So you have some root --...--> (some object) --"A"--> (object named A) and (object named B) But no property actually named "B" exists in any object that contains a reference to (object named B)? Then (object named B) is garbage liable to be collected by the next GC. If you have a property named "B" in some object whose value refers to (object named B), then the question is, what connects the object containing the property named "B" to the live object graph marked by the GC, starting from the root set? > Is the best way to inform the GC of this to use JS_AddRoot(B) in A's > constructor > (and a corresponding JS_RemoveRoot(B) in A's destructor)? If (object named B) really has no property named "B" in a live object protecting it, then you could use roots. But if A has a custom class, and if that class uses the JSCLASS_HAS_PRIVATE flag, then you should rather use JSCLass.mark to call JS_MarkGCThing on the pointer to the (object named B) that is kept in the private data structure. Another alternative is to use JSCLASS_HAS_RESERVED_SLOTS and JS_Get- and JS_SetReservedSlot. Then the GC will scan the reserved slot for you, but the reserved slot has no named property associated with it. The trade-off between using JSClass.mark and reserved slots is easy: if you have private data, and your native code uses the pointers in that private data, such as the JSObject *objNamedB; (or whatever you name the private data struct member), then you want to use JSClass.mark. If your class has no private data, or even if it does, but your native code has no need for a JSObject *objNamedB; member in that struct, then you may as well use a reserved slot. /be .