Preventing Garbage Collection in the Spidermonkey

2019-09-05 13:02发布

问题:

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?

回答1:

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.