具有持久功能的JavaScript中的分割故障
Segmentation fault in javascript with persistent function
动态库是使用某些C文件创建的。此动态库由C 函数使用。其中一个C函数称为C 功能。反过来将调用JavaScript函数回调。我在C 功能中得到分割故障。
这是代码,在test.h中,
#ifdef __cplusplus
extern "C" {
#endif
void cppFunc(void);
#ifdef __cplusplus
}
#endif
这是test.c
中的代码#include "test.h"
void cFunc(void){
/* some code */
cppFunc();
}
这是test.cpp
中的代码#include "test.h"
static Nan::Persistent<v8::Function> callback;
void storeFunc(const v8::FunctionCallbackInfo<v8::Value>& args){
if(args[0]->IsFunction()){
Local<Function> cb = Local<Function>::Cast(args[0]);
callback.Reset(cb);
}
}
void cppFunc(void){
Isolate *isolate = Isolate::GetCurrent();
Local<Function> c_back = Local<Function>::New(isolate, callback);
Nan::MakeCallback(Nan::GetCurrentContext()->Global(), c_back, 0, {});
}
void init(Handle<Object> exports, Handle<Object> module)
{
NODE_SET_METHOD(exports, "FuncStore", storeFunc);
}
NODE_MODULE(test, init)
test.Node是通过使用" Node-GYP Configure build"命令创建的。没有显示错误或警告。
以下是我的test.js文件,
var sample = require("./build/Release/test.node");
sample.FuncStore(function(){
console.log("Callback has been called!!");
});
在" node test.js"执行test.js上,当cfunc()调用cppfunc()时会发生分割故障。细分故障正好发生在" local C_back = local :: new(孤立,回调)"的行中。在cppfunc()中。
原因是什么?
一种可能性是指针"隔离"是NULL
。访问没有值或无效值的指针可能会导致分割故障。您应该通过执行以下操作检查它的存在:
Isolate *isolate = Isolate::GetCurrent();
if (isolate) {
//it is now safe to use isolate
Local<Function> c_back = Local<Function>::New(isolate, callback);
Nan::MakeCallback(Nan::GetCurrentContext()->Global(), c_back, 0, {});
}
else {
//possibly generate your own warning here
}
作为附带说明,似乎是孤立:: getCurrent被弃用https://github.com/nodejs/nodejs/node/commit/409d413363
相关文章:
- 如何在C++中从字符串中分割字符
- C++映射分割错误(核心转储)
- C++为线程工作动态地分割例程
- 如何在本地机器上运行c++和javascript客户端代码(hackerbank风格)
- 如何将C++闭包与变量参数同时重用——类似于JavaScript
- 由cin中的字符串中未捕获空白引起的分割错误
- 删除映射和分割错误中的一个过去结束元素
- 嘿,我正在尝试将此c ++转换为javascript,但有些东西不起作用
- 在指向函数中读取变量时出现分割错误
- 在链表中的第 n 位插入显示分割错误
- C++中的 JavaScript 样式闭包
- 较高值 n 的分割错误(例如 n=999997)
- OpenCV(C++)处理来自JavaScript / Web Assembly的图像数据
- 在C++中嵌入 Mozilla 的 JavaScript 引擎
- Javascript 找不到使用 emscripten 编译的导出 WASM 函数
- 是否可以制作没有内部分割的cgal 3d多多马因网格?
- 尝试通过memcpy复制大尺寸浮点向量时的分割错误
- 在Javascript和C++中计算MD5哈希的不同结果
- 分割错误:向量中的擦除功能
- 具有持久功能的JavaScript中的分割故障