Subj : Memory management of contexts etc. To : netscape.public.mozilla.jseng From : Heath Raftery Date : Tue Sep 23 2003 11:54 pm Hi, Not long ago folks on this newsgroup saved my sanity but helping me out in writing a PAC file parser using SpiderMonkey. It is all working great (woohoo!), but I've got some followup questions about style and proper contex management. My code structure looks like this: main() { daemon(0, 0); JSFunction* compiled = compile(arg1) for(;;) { switch(fork()) { case 0: result = execute(compiled, arg2) /* act on result */ break; } } JS_DestroyRuntime(rt); } JSRuntime *rt; JSObject *global; JSClass global_class = { "global", 0, JS_PropertyStub,JS_PropertyStub,JS_PropertyStub,JS_PropertyStub, JS_EnumerateStub,JS_ResolveStub,JS_ConvertStub,JS_FinalizeStub }; JSFunction* compile(arg1) { JSContext *cx; rt = JS_NewRuntime(1L * 1024L * 1024L); cx = JS_NewContext(rt, 4L * 1024L); global = JS_NewObject(cx, &global_class, NULL, NULL); JS_InitStandardClasses(cx, global); JSFunction *compiledFunc = JS_CompileFunction(cx, global, ...); //***can't do this here*** //JS_DestroyContext(cx); return compiledFunc; } char* execute(JSFunction *compiledScript, const char *arg2) { JSContext *cx; cx = JS_NewContext(rt, 4L * 1024L); JS_CallFunction(cx, global, compiledScript, 2, args, &rval); str = JS_ValueToString(cx, rval); JS_DestroyContext(cx); return JS_GetStringBytes(str); } Now, note that I can't destroy the context in compile(), because the function will then fail to run in execute(). Thus, I never actually get the chance to destroy it! Is this particularly undesirable? Is my basic runtime/context use scheme flawed? It seems to run fine, but I haven't done any intense analysis of memory use or profiling. There does appear to be a bit of a pause (a couple of seconds) while the compiled function is executed, but not knowing how long the script should take to execute in the first place, it is a bit hard to nail down a bottleneck. Appreciate any comments. Heath -- Heath ________________________________________________________ | *Nothing is foolproof to a sufficiently talented fool* | | _\|/_ | |________________________________________m(. .)m_________| .