Question

According the Spidermonkey's User Guide

https://developer.mozilla.org/En/SpiderMonkey/JSAPI_User_Guide

... a jsval by itself does not protect its referent from the garbage collector...

My understanding of this statement is that if we create a JSString by, say, JS_NewStringCopyZ(), the value returned can be gc'ed at anytime. e.g.

JSString *str=JS_NewStringCopyZ(cx, "hello world");
JS_GC(cx);
//..now my "hello world" JSString is gone

So how do we prevent the str above from being gc'ed? I notice there is a JS_EnterLocalRootScope () function that I can call at the start of my JSNative function. Unfortunately, it is now deprecated. Then what should be the proper way to prevent gc?

Was it helpful?

Solution

I think this is a duplicate of Garbage collector issues on spidermonkey… JS_AnchorPtr()? -- the answer there shows how to anchor pointers to keep them live across a GC when using SpiderMonkey as a shared library. If that doesn't answer your question, please edit to clarify.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top