Subj : Re: GC questions / debugging To : James Turner From : Brendan Eich Date : Thu Dec 11 2003 10:32 am James Turner wrote: > A couple of hopefully quick questions about GC (in a threaded program, > if that's relevant) > > - are functions GC things? I'm storing lots of JSFunction pointers for > lookup at a later time (potentially much later), so at the moment I'm > JS_AddRooting them, but I don't know if this is pointless or not. Note > that in practice nearly all these functions are anonymous. It's worse than pointless, it's a bug that will crash your program during the mark phase, sooner or later. A JSFunction is the type of private data of a function object. Only objects, strings, and jsdoubles allocated from the GC heap are GC-things (jsdouble, IEEE double, can be a stack-allocated temporary usually, but for doubles tagged as jsvals and stored (indirectly) in property slots, GC-heap-allocated jsdouble GC-things are required). You should root a jsval, a JSObject*, a JSString*, or a jsdouble* that came from the return value of a JS_NewDouble call. And in case the docs are unclear, a root is a pointer known by its address, registered by reference, so you pass the address of one of those types (e.g., jsval v; JS_AddRoot(cx, &v) -- use the unary & operator). > - Are there any general tips on debugging crashes inside the GC? I've > been slowly going through my program fixing various things to be > rooted, adding BeginRequest/EndRequest sections around calls into > SpiderMonkey, and so on. I've fixed most of the issues, but not all. > What I'm left with is infrequent segfaults inside the GC's mark phase. > (I'm not bothering to post a backtrace becuase I assume the segfault > is just a symptom of some prior bug on my part) Probably due to your rooting private JSFunction structs that are not GC-things. > If there's anything I can set (compile flags, callbacks) or tools I > could run (I've used valgrind under linux, but it doesn't think > anything is wrong), I'd be grateful. GC_MARK_DEBUG is useful to find why something is not being collected, even though you think it ought to be garbage (leak diagnosis, IOW). You can also use GC_MARK_DEBUG-enabled code to dump the heap. It sounds like you've read http://www.mozilla.org/js/spidermonkey/gctips.html already, so you're probably using local roots to good effect. /be .