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

  • Home
  • SEARCH
  • 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 1039373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:05:30+00:00 2026-05-16T15:05:30+00:00

I got a question regarding C++ Object & Javascript Object life cycle mis-synchronization and

  • 0

I got a question regarding C++ Object & Javascript Object life cycle mis-synchronization and hope here your gurus could help me out.

Specifically, I am embedding SpiderMonkey in my C++ program. I am not sure if my approach is right or not. What I am doing is like

(1) C++ program loads a Javascript file and call an entry function.

(2) In the entry function, some C++ functions are called to initialize the program. During these calls, some C++ objects will be created.

(3) Based on user interaction, corresponding script function will be called as event handler.

This approach works, but with one problem (there could be unidentified more 🙂

That is,

In my JS event handler function, I need to fetch a C++ object to the Javascript context and call it member function. For this, in my C++ fetching function, I check if its corresponding JS peer object has been created or not. If not, I use JS_NewObject to create one and return it. Then the JS function could call native function on it.

The problem is that in some cases, the result of such native function call will lead the death of the C++ object. However, I can not find a way to notify the JS context to delete its JS peer object too. I failed to find a JSAPI function to do so.

In my current program, all the JS objects created using JS_NewObject are destroyed when finally the JS runtime is destroyed.

I guess this has something do with SipderMonkey’s “garbage collection”. But I have not found a good guide yet. Many thanks for any suggestionto

  • 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-05-16T15:05:30+00:00Added an answer on May 16, 2026 at 3:05 pm

    JS is a GC’d environment so you can’t simply “delete” a GC allocated object. There are basically 2 options you can take:

    1. Make your C++ object be dependent on the JS wrapper object, if you were using refcounting for instance you would increment the C++ object’s ref when you created a wrapper, and decrement the ref in the wrapper objects finalizer.

    2. When you destroy the C++ object, fetch the wrapper object (if it exists) as clear the reference to the C++ object. All your callbacks will now need to null check prior to using the C++ object, but you won’t crash (you could throw a JS exception in response perhaps?)

    In most cases option 1 is what users expect.

    I’d point to the required API but i don’t know the SM API (I know the JSC API instead, but they same concepts apply)

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

Sidebar

Related Questions

I got another question regarding jQuery's architecture. $('div') constructs a new jQuery object: $('div')
Got a question regarding best practices for doing parallel web service calls, in a
I've got a question regarding a SQL-select-query: The table contains several columns, one of
I've got another question regarding to basic Android programming: How can I access the
I've got a question regarding the Telerik RadGrid control client side binding. I want
I am currently reading Dive into HTML5 and I've got a question regarding the
I've got a pretty straight forward question regarding viewing text in Firefox. My application
I've got a probably embarrassingly easy question regarding Hibernate: When I've got a persisted
While reading the answers to this question I got a doubt regarding the default
I've got a quick question regarding the use of repositories. But the best way

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.