Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

jsval and Garbage Collection

18 views
Skip to first unread message

Masquerade

unread,
Feb 28, 2012, 12:06:04 PM2/28/12
to
My JSNative functions often call many other JSAPI and obtain many
jsval - more than the # of argv elements. So I cannot use the argv
as the sole rooted storage for my jsval. I am not sure if this is a
correct approach to get more rooted tmp storage:

static jsval * getJsvalTmp(JSContext *cx) {
static const int jsvalTmpLen=5;
static jsval jsvalTmp[jsvalTmpLen];// static var initialized only
when function first called.
static bool rooted=false;
if (!rooted) {
rooted=true;
for (int i=0; i<jsvalTmpLen; i++) {
jsvalTmp[i]=JSVAL_VOID;
JS_AddValueRoot(cx, &jsvalTmp[i]);
}
}
return jsvalTmp;
}

void some_other_function() {
jsval* rootedTmp=getJsvalTmp(cx) {
rootedTmp[0]= OBJECT_TO_JSVAL(..);
rootedTmp[1]= STRING_TO_JSVAL(...);
....
// reuse the tmp rooted jsval, would this cause the previous
JSObject/JSString gc'ed?
rootedTmp[0]= OBJECT_TO_JSVAL(..);
rootedTmp[1]= STRING_TO_JSVAL(...);
}
My getJsvalTmp() creates a permanently rooted jsval array on the first
invocation and returns a pointer to it everytime I need some tmp
rooted jsval. They serves as a reusable rooted tmp jsval storage
shared by my functions within the compilation unit.

I am not sure if my understanding is correct: now my jsvalTmp array is
rooted and any JSObject/JSString it points to is rooted and won't be
gc'ed. If yes, do I need to explicitly remove these JSObject/JSString
if I assign another JSObject/JSString to my jsval array?

When will a JS_RemoveValueRoot call needed?
Message has been deleted
0 new messages