Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7739499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:32:20+00:00 2026-06-01T08:32:20+00:00

I am embedding Spidermonkey in my C++ application. I need to implementing some custom

  • 0

I am embedding Spidermonkey in my C++ application. I need to implementing some custom Javascript functions in native C++ that pass around a jsval. I need to guard the jsval against accidental garbage collection. Is it proper for me to do this:

(1) In an init routine:

static jsval vp; // a STATIC variable, value unknown
JSBool init((JSContext *cx, uintN argc, jsval *vp) {
   JS_AddValueRoot(cx,  &vp);
}

(2) In one c++ function implementing the Javascript function setter():

JSBool setter(JSContext *cx, uintN argc, jsval *vp) {
   ...
  vp=...;// set to some JSObject and hopefully makes any previous JSObject available for gc

}

(3) In a second C++ function call within the same compilation unit implementing the Javascript function getter():

JSBool getter(JSContext *cx, uintN argc, jsval *vp) {
  jsval somethingelse = vp; //directly retrieve the static value stored by setter()
  ....
}

My Javascript script uses the function calls like this:

init();
setter(...);
some_other_function_call_that_causes_gc();
getter();
setter(...);
some_other_function_call_that_causes_gc();
getter();
....
some_other_function_call_that_causes_gc();
setter(...);
some_other_function_call_that_causes_gc();
getter();

Note that I never call JS_RemoveRoot() since the static jsval vp is a permanent storage for my jsval passed between 2 function calls. And, I keep setting new values into the gc rooted static variable vp in my setter(), assuming any previouis JSObject stored in jsval will be available for garbage collection.

Are these proper ways to create gc rooted temporary variables that can be passed across function call? In particular, is the way my setter() replacing a previous JSObject a proper way to make an existing JSObject available for gc (i.e. no memory-leak/crash).

EDIT: The reason why I believe Garbage collection is an issue is that:

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

Under Section JSAPI Concepts, Javascript Values:

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

https://developer.mozilla.org/en/SpiderMonkey_Garbage_Collection_Tips

Example 3 says “Root as you go” and shows how a jsval must be assigned to a root.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:32:21+00:00Added an answer on June 1, 2026 at 8:32 am

    There is no need to add a root for something like a jsval. You simply need to maintain a reference to the value in your script. SpiderMonkey GC is refrence count based, therefore your jsvals will not dissapear as long as they are referred to in the current scope:

    var x = 5;

    callNativeFun(x);

    function foo() { throw true; }

    print(nativeFunRef());

    //script ends here. X and FOO will be garbage collected as will callNativeFun and nativeFunRef js function signatures.

    In the above code example both x and foo are held referent by the global object.

    As long as the jsval pointer content is defined in the script it will never reach GC. Make sure your binary code never uses the value after its life is over; that is when you are done with it in the script and call delete on it or set its value to undefined by value or proxy of scope termination. If you forsee any of these negative interactions you should be setting a GC root on that jsval.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm embedding Ruby in C and I need to pass a struct created in
I am embedding tracking images within emails that are being sent from a custom-built
I am embedding IronPython in a C# program. I understand that I need to
I'm embedding IronPython in my C# application. For some reason I'm having trouble loading
I'm embedding Mozilla's SpiderMonkey in my application and would like to use its new
I am embeding SpiderMonkey (mozila javascript) interpreter/engine in my C/C++ application. i programatically loading
I am embedding a external js file on some domains and I need to
When embedding a JavaScript interpreter (Rhino) into a Java application (to be able to
I'm embedding a flash swf into an html page and setting wmode=transparent. I need
I am embedding Lua into a C/C++ application. Is there any way to call

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.