这是我用 C++ 编写并使用 node-gyp 构建的 node.js 插件模块。当 StoreFunction 我试图存储一个指向函数的指针,以便以后可以使用它
当我稍后尝试在 InvokeFunction 中调用它时,我得到一个分段错误。如果我检查了两个函数中的指针(使用 cout)它们是相同的值,这让我感到困惑。
所以我猜测调用两个函数之间调用上下文的变化或者我不明白我指的是什么。
所有(ummmmmm)指针都非常感谢我在这里收到的问题........
#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);
当然还有一些调用js…………
var myaddon = require('../build/Release/someaddonmodule');
myaddon.StoreFunction(function(data){
console.log("Called back: "+data);
});
myaddon.InvokeFunction(); //causes a segmentation fault