Here is a node.js addon module I've written in C++ and built using node-gyp. When StoreFunction I am trying to store a pointer to the function so I can use it later
When I try to invoke it later though in InvokeFunction I get a Segmentation fault. What baffled me if I examined the pointer in both functions (using cout) they are the same value.
So I'm guessing either the change of invoking context changes between calling the two functions or I don't understand what I'm pointing to.
All (ummmmmm) pointers gratefully received on my problem here..............
#include <node.h> #include <v8.h> using namespace v8; v8::Persistent<v8::Function> callbackFunction; Handle<Value> StoreFunction(const Arguments& args) { HandleScope scope; callbackFunction = *Local<Function>::Cast(args[0]); return scope.Close(Undefined()); } Handle<Value> InvokeFunction(const Arguments& args) { HandleScope scope; Local<Value> argv[1] = { String::New("Callback from InvokeFunction")}; callbackFunction->Call(Context::GetCurrent()->Global(), 1, argv); return scope.Close(Undefined()); } void init(Handle<Object> target) { NODE_SET_METHOD(target, "StoreFunction", StoreFunction); NODE_SET_METHOD(target, "InvokeFunction", InvokeFunction); } NODE_MODULE(someaddonmodule, init);
And of course some calling js...........
var myaddon = require('../build/Release/someaddonmodule');
myaddon.StoreFunction(function(data){
console.log("Called back: "+data);
});
myaddon.InvokeFunction(); //causes a segmentation fault