Subj : JS_AddNamedRoot() and finalizer To : netscape.public.mozilla.jseng From : Matthew Mondor Date : Mon Aug 23 2004 10:00 am Hi, Using SpiderMonkey from C, I am creating a test class called API which has to be almost immutable (although not totally as immutable as a scealed one, since there are a few voluntary read-write properties as part of it). This class object is protected from GC collection by using JS_AddNamedRoot(), and normally then unlinked using JS_RemoveRoot(). However, in an attempt to clean up the program, and using a function similar to JS_CreateStandardClasses() to create this class, I attempted to also include the JS_RemoveRoot() as part of a custom finalizer method of that class, instead of needing a corresponding function which only calls JS_RemoveRoot(), or calling it explicitely, before calling JS_DestroyContext() and JS_DestroyRuntime() before application exit. However, this causes the following message to be output: JS API usage error: the address passed to JS_AddNamedRoot currently holds an invalid jsval. This is usually caused by a missing call to JS_RemoveRoot. The root's name is "API_Prototype". Assertion failure: root_points_to_gcArenaPool, at jsgc.c:972 Abort (core dumped) So it seems that the finalizer method of a class may not use JS_RemoveRoot()... I suspected that perhaps the two parameters passed to the finalizer aren't exactly the expected ones, causing the call to fail, but then realized that the finalizer function never gets called at all. Would the finalizer not be invoked because of the fact that the class has JSPROP_SHARED properties? Or is it simply never normally triggered by JS_DestroyContext() the objects are on, but only called by the GC as required at runtime? In any case is there another recommended method to "register" a finalizer function to be called at context shutdown? If this can't be done it isn't the end of the world however since I can simply have internal implementation of required finalization code calling JS_RemoveRoot() for required objects before shutdown, but the above tests made me wonder... Thanks, Matt .