防止Spidermonkey Javascript引擎中的垃圾收集 [英] Preventing Garbage Collection in the Spidermonkey Javascript Engine
问题描述
根据Spidermonkey的用户指南
https://developer.mozilla.org/En/SpiderMonkey/JSAPI_User_Guide
...一个jsval本身不会保护其对垃圾
收集器的引用...
我对这条语句的理解是,如果我们通过创建一个JSString比如JS_NewStringCopyZ(),返回的值可以在任何时候被gc'ed。例如
JSString * str = JS_NewStringCopyZ(cx,hello world);
JS_GC(cx);
// ..现在我的hello worldJSString不见了
阻止上面的 str 被gc'ed?我注意到有一个 JS_EnterLocalRootScope ()函数,我可以在我的JSNative函数开始时调用它。不幸的是,它现在已被弃用。那么应该怎么防止gc的正确方法呢?
解决方案 我认为这是 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. 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? 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. 这篇关于防止Spidermonkey Javascript引擎中的垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
JSString *str=JS_NewStringCopyZ(cx, "hello world");
JS_GC(cx);
//..now my "hello world" JSString is gone