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 8718835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:38:46+00:00 2026-06-13T06:38:46+00:00

How would you save a javascript supplied callback in a v8 wrapped object for

  • 0

How would you save a javascript supplied callback in a v8 wrapped object for future use not only in the current function call. Essentially i want to create a javascript object in C++ and when created with new Object() supply a function callback. Then use that callback throughout the c++ objects life. See example below:

The issue im having is when I try to use the Handle object in a different static function it seg faults.

In node js file:

var Object = require("./customModule");
var obj = new Object(function(){console.log("Callback called...")})

// Emit callback
obj.emitCallback();

In c++ module header

class Object : public node::ObjectWrap {

public:

    static void Init(v8::Handle<v8::Object> target);

    Object();

protected:

    v8::Handle<v8::Function> m_faceDetectCallback;

    static v8::Handle<v8::Value> New(const v8::Arguments& args);

    static v8::Handle<v8::Value> onEmitCallback(const v8::Arguments& args);
}



v8::Handle<v8::Value> Object::New(const v8::Arguments& args) {

    HandleScope scope;

    Object* obj = new Object();
    obj->Wrap(args.This());

    obj->m_faceDetectCallback = Handle<Function>::Cast(args[0]);

    //obj->m_faceDetectCallback = v8::Persistent<Function>::Cast(args[0]);

    // Works fine here.
    const unsigned argc = 1;
    Local<Value> argv[argc] = { Local<Value>::New(String::New("hello world")) };
    obj->m_faceDetectCallback->Call(Context::GetCurrent()->Global(), argc, argv);

    return args.This();
}

static v8::Handle<v8::Value> Object::onEmitCallback(const v8::Arguments& args){
    HandleScope scope;

    Object* obj = ObjectWrap::Unwrap<Object>(args.This());

    const unsigned argc = 1;
    Local<Value> argv[argc] = { Local<Value>::New(String::New("hello world")) };

        //!! Segfaults here
    if(obj->m_faceDetectCallback->IsCallable()){
        //obj->m_faceDetectCallback->Call(Context::GetCurrent()->Global(), argc, argv);
    }


    return scope.Close(v8::String::New("Start called"));
}
  • 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-13T06:38:48+00:00Added an answer on June 13, 2026 at 6:38 am

    You need to use v8::Persistent instead of a standard handle. Handle is the base class for Local and Persistent so by doing the cast that you are doing, you are grabbing a pointer to the v8::Function but not doing anything that would tell V8 not to garbage-collect it.

    With this in your class:

    v8::Persistent<v8::Function> m_faceDetectCallback;
    

    and assigned with

    obj->m_faceDetectCallback = v8::Persistent<v8::Function>::New(args[0]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am studying javascript and I've got some problems. I would like to save
This would save enormous amounts of time, as Firebug is what I use to
i want to save javascript object in a place, so if selected row is
I would like to save the current state of an UIWebView to disk in
I would love to figure out how to save a event.target for later use.
I would like to save a substring to a javascript variable using regex unless
am learning javascript and not at a great rate, am trying to use the
I have a save function in my JavaScript that is responsible for saving some
When using ModelForm Within forms.py it would save a lot of time, since there
So.. I have a buffer with MP3 data (If I would save this buffer

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.