Subj : Re: Garbage Collection Woes To : Stephen Jones From : Brendan Eich Date : Fri May 02 2003 01:21 pm Stephen Jones wrote: >Hi Brendan, thanks for the feedback. >Iterating over all contexts, I am definately seeing different objects GC'ed >for each context I pass in. >Should I assume: > >1. Objects are being incorrectly GC'ed when I pass in different contexts, or >2. Objects that are being GC'ed when passing through one context, release >other objects that are GC'ed when the next context is iterated. > >I'm definately passing the address of JSObjects to be rooted. I have one >base C++ class that handles this. > > See dbradley's followup. >I've temporarily solved this problem by ref-counting my objects and calling >RemoveRoot when the ref-count is zero, but I'd like to figure out a real >solution. > > If you have a JSObject * member of some data structure, let's call it myDataStruct->mJSObject, you must pass it to JS_AddNamedRoot by reference: JS_AddNamedRoot(cx, &myDataStruct->mJSObject, "myDataStruct->mJSObject"); If you have several calls to this function for the same address of a JSObject pointer, then you are wasting cycles on all but the first call, and indeed, you need a reference count to avoid removing the root until the count goes to zero. But you should be calling JS_AddNamedRoot exactly once per unique address of a stable JSObject pointer, and JS_RemoveRoot before that storage becomes unstable (goes free, gets recycled). /be >Thanks, >- S > >"Brendan Eich" wrote in message >news:3EB1C5E9.60802@meer.net... > > >>Stephen Jones wrote: >> >> >> >>>One more piece of information . . . this only seems to happen to objects >>>created in contexts other than the global context . . . am I doing >>>something wrong when executing GC? This is the code I am using . . . >>> >>>while( ( theCurrentContext = JS_ContextIterator( m_JSRunTimePtr, >>>&theContextIterator ) ) != NULL ) >>>{ >>> JS_GC( theCurrentContext ); >>>} >>> >>>Is the "live-thing" graph different for different contexts? >>> >>> >>> >>> >>No. >> >>Therefore you don't need to call the GC more than once per runtime, >>using any context created in that runtime. >> >>You shouldn't add a bunch of JS_AddNamedRoot calls without knowing why >>you need each one. Remove all that you don't believe you need. >> >>Make sure you aren't passing object pointers or jsvals by value to >>JS_Add*Root. You have to pass the address of a JSObject* or jsval to >>register a root. >> >>/be >> >> >> > > > > .