Subj : Custom C++ objects with references and garbage collecting in spidermonkey To : netscape.public.mozilla.jseng From : Mads Bondo Dydensborg Date : Fri Mar 18 2005 10:34 am Hi all I have been searching for an answer to this question, and I sure hope someone can help me out. Say I have the following classes class A { ... } class B { .... public: class A & getA(int i); } and that both are exposed to the JS engine, using the private data to store a pointer to the object. Now, using C++ code, these classes have the problem that the following code is wrong: B b = new B(); A & a = b.getA(0); delete b; use(a); // Error, a is no longer valid. Now, these classes may be broken - I did not design them and can not change them on a source level - but avoiding these situation are pretty easy in C++, because of the way the classes are used (B is a sort of a container for A, and it does make sense). However, say I have java code like this, where the classes have been exposed using the private data fields: var B = new B(); var a = b.getA(0); // From this point on, b is never referenced /// code, here /// The JS decides to GC b print( a ); // UPS! a contains a pointer to an object embedded in b, // that is now gone! How do I solve this problem? Basically I want to say to the GC: "Do not clean B before a have been cleaned". I have thought about Rooting b in a, but you can have many a's coming out from b at the same time: var B = new B(); var a = b.getA(0); var anothera = b.getA(1); I don't think there is an easy way to us GC_AddRoot to that? Or rather, if I call GC_RemoveRoot, when a is finalized, I have the problem that anothera needs b to live on. I want to say to the GC: "Do not clean B before a and another a have been cleaned" From reading the docs, it seems to me, that one perhaps should make a and another a properties of b? So, I should set a and anothera as properties of b, whenever b.getA(int) returns an a? Does that sound right? Any help will be greatly appriciated. A pointer to some docs about how to use the SpiderMonkey GC "right" would be also be appreciated. Thanks in advance, Mads .