Subj : Re: Custom C++ objects with references and garbage collecting in To : Mads Bondo Dydensborg From : Brendan Eich Date : Fri Mar 18 2005 10:03 am Mads Bondo Dydensborg wrote: > > I want to say to the GC: "Do not clean B before a and another a have > been cleaned" Then you need a and anothera to refer to b (I think you mean b, not B, here), each using a strong reference. A strong reference could be a property in each a, anothera, etc. -- call it parent -- such that a.parent == b. You can set such a property with JS_SetProperty, or if you want it to be readonly and permanent, JS_DefineProperty. If you don't want a property, you could have a rooted pointer in the private data of each a, say a.[[parent]] (I'm making up notation to distinguish the internal property [[parent]] from the property parent proposed in the last paragraph). You could root this pointer using JS_AddNamedRoot, remembering to remove it with JS_RemoveRoot when a is being finalized. Or, and this is better, you could have the JSClass corresponding to the C++ class A define a mark hook. Note: if you do not use JSClass.mark, but choose to add explicit GC roots using JS_AddNamedRoot, then if b refers to each a, anothera, etc. with its own rooted pointers, you have created cyclic pairs of roots: b.[[child-0]] -> a and a.[[parent]] -> b. Furthermore, if you remove roots only in finalizers, these roots guarantee that no object in this graph can ever be collected. That's one reason why using JSClass.mark is better. Another is that it uses less memory; it may also use fewer cycles. > From reading the docs, it seems to me, that one perhaps should make a > and another a properties of b? No, that would keep each a alive so long as b is not garbage, but you want the other way round (or perhaps you want both). > Any help will be greatly appriciated. A pointer to some docs about how > to use the SpiderMonkey GC "right" would be also be appreciated. I assume you've read all the docs available at http://www.mozilla.org/js/spidermonkey/ -- you ought to read up on garbage collection too. /be .