两次调用某些回调函数会导致分段错误:Nan
Invoking some callback function twice leads to Segmentation fault: Nan
我正在使用 nbind - GitHub link for most thing and Nan - GitHub link 编写 C++ 插件来异步调用回调。当我只调用一次回调时,它工作得很好。但是当我两次调用回调时,它会给出 Segmentation fault (core dumped)
。使用 gdb
找不到错误。这是JS和C++代码(使用node-gyp configure build
编译):
//main.js code
var nbind = require('nbind');
var lib = nbind.init().lib;
lib.HeaderExample.callJS(function(a) {
console.log("result" + a);
});
lib.HeaderExample.startThread();
lib.HeaderExample.startThread();
C++ 插件代码
//c++ code
class CallbackRunner : public Nan::AsyncWorker {
public:
CallbackRunner(Nan::Callback *callback)
: AsyncWorker(callback) {}
void Execute () {}
void HandleOKCallback () {
std::cout << "running HandleOKCallback in thread " << std::this_thread::get_id() << std::endl;
Nan::HandleScope scope;
v8::Local<v8::Value> argv[] = {
Nan::New<v8::Number>(10)
};
callback->Call(1, argv);
}
};
class HeaderExample {
public:
static void callJS(nbind::cbFunction &callback) {
std::cout << "running callJS in thread " << std::this_thread::get_id() << std::endl;
m_onInitialisationStarted = new nbind::cbFunction(callback);
Nan::Callback *callbackNan = new Nan::Callback(m_onInitialisationStarted->getJsFunction());
runner = new CallbackRunner(callbackNan);
}
static void startThread() {
std::cout << "it is here";
std::thread threadS(some);
threadS.join();
}
static void some() {
std::cout << "running some in thread: " << std::this_thread::get_id() << std::endl;
if(runner){
AsyncQueueWorker(runner);
}
}
inline static nbind::cbFunction *m_onInitialisationStarted = 0;
inline static CallbackRunner *runner;
};
不能让两个线程同时调用同一个 V8 实例。您需要仔细锁定以确保在任何时间点只有一个线程与 V8 交互。
您的 class 使用 AsyncQueueWorker
调用 CallbackRunner
,但 AsyncQueueWorker
在回调完成后调用 AsyncExecuteComplete
,后者又调用 worker->Destroy()
。请参阅 nan.h
中的 AsyncQueueWorker
代码:
inline void AsyncExecute (uv_work_t* req) {
AsyncWorker *worker = static_cast<AsyncWorker*>(req->data);
worker->Execute();
}
inline void AsyncExecuteComplete (uv_work_t* req) {
AsyncWorker* worker = static_cast<AsyncWorker*>(req->data);
worker->WorkComplete();
worker->Destroy();
}
inline void AsyncQueueWorker (AsyncWorker* worker) {
uv_queue_work(
uv_default_loop()
, &worker->request
, AsyncExecute
, reinterpret_cast<uv_after_work_cb>(AsyncExecuteComplete)
);
}
worker->Destroy()
将删除 CallbackRunner
class,以及您提供给其构造函数的 Nan::Callback
。这就是为什么在尝试第二次调用此回调时出现分段错误的原因。
您最好将 class 基于 Nan::AsyncProgressQueueWorker
而不是 Nan::AsyncWorker
。 AsyncProgressQueueWorker
继承了 AsyncWorker
并且它允许您像 AsyncWorker
一样从主线程安排工作,但是它为您提供了 ExecutionProgress
class 允许您在原始计划作业为 运行.
时使用任何线程多次回调主线程
Nan::AsyncProgressQueueWorker
已添加到 NAN 版本 2.8.0
我正在使用 nbind - GitHub link for most thing and Nan - GitHub link 编写 C++ 插件来异步调用回调。当我只调用一次回调时,它工作得很好。但是当我两次调用回调时,它会给出 Segmentation fault (core dumped)
。使用 gdb
找不到错误。这是JS和C++代码(使用node-gyp configure build
编译):
//main.js code
var nbind = require('nbind');
var lib = nbind.init().lib;
lib.HeaderExample.callJS(function(a) {
console.log("result" + a);
});
lib.HeaderExample.startThread();
lib.HeaderExample.startThread();
C++ 插件代码
//c++ code
class CallbackRunner : public Nan::AsyncWorker {
public:
CallbackRunner(Nan::Callback *callback)
: AsyncWorker(callback) {}
void Execute () {}
void HandleOKCallback () {
std::cout << "running HandleOKCallback in thread " << std::this_thread::get_id() << std::endl;
Nan::HandleScope scope;
v8::Local<v8::Value> argv[] = {
Nan::New<v8::Number>(10)
};
callback->Call(1, argv);
}
};
class HeaderExample {
public:
static void callJS(nbind::cbFunction &callback) {
std::cout << "running callJS in thread " << std::this_thread::get_id() << std::endl;
m_onInitialisationStarted = new nbind::cbFunction(callback);
Nan::Callback *callbackNan = new Nan::Callback(m_onInitialisationStarted->getJsFunction());
runner = new CallbackRunner(callbackNan);
}
static void startThread() {
std::cout << "it is here";
std::thread threadS(some);
threadS.join();
}
static void some() {
std::cout << "running some in thread: " << std::this_thread::get_id() << std::endl;
if(runner){
AsyncQueueWorker(runner);
}
}
inline static nbind::cbFunction *m_onInitialisationStarted = 0;
inline static CallbackRunner *runner;
};
不能让两个线程同时调用同一个 V8 实例。您需要仔细锁定以确保在任何时间点只有一个线程与 V8 交互。
您的 class 使用 AsyncQueueWorker
调用 CallbackRunner
,但 AsyncQueueWorker
在回调完成后调用 AsyncExecuteComplete
,后者又调用 worker->Destroy()
。请参阅 nan.h
中的 AsyncQueueWorker
代码:
inline void AsyncExecute (uv_work_t* req) {
AsyncWorker *worker = static_cast<AsyncWorker*>(req->data);
worker->Execute();
}
inline void AsyncExecuteComplete (uv_work_t* req) {
AsyncWorker* worker = static_cast<AsyncWorker*>(req->data);
worker->WorkComplete();
worker->Destroy();
}
inline void AsyncQueueWorker (AsyncWorker* worker) {
uv_queue_work(
uv_default_loop()
, &worker->request
, AsyncExecute
, reinterpret_cast<uv_after_work_cb>(AsyncExecuteComplete)
);
}
worker->Destroy()
将删除 CallbackRunner
class,以及您提供给其构造函数的 Nan::Callback
。这就是为什么在尝试第二次调用此回调时出现分段错误的原因。
您最好将 class 基于 Nan::AsyncProgressQueueWorker
而不是 Nan::AsyncWorker
。 AsyncProgressQueueWorker
继承了 AsyncWorker
并且它允许您像 AsyncWorker
一样从主线程安排工作,但是它为您提供了 ExecutionProgress
class 允许您在原始计划作业为 运行.
Nan::AsyncProgressQueueWorker
已添加到 NAN 版本 2.8.0