Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

Dynamic library is created using some C files. This dynamic library is used by a C++ function. One of the C-functions calls a C++ function. Which in turn will invoke a Javascript Function Callback. I am getting segmentation fault in C++ function.

Here is the code, In test.h,

#ifdef __cplusplus
extern "C" {
#endif
      void cppFunc(void);
#ifdef __cplusplus
}
#endif

Here is the code in test.c

#include "test.h"
void cFunc(void){
/* some code */
    cppFunc();
}

Here is the code in 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 is created by using "node-gyp configure build" command. No errors or warnings are shown.

Following is my test.js file,

var sample = require("./build/Release/test.node");
sample.FuncStore(function(){
    console.log("Callback has been called!!");
});

On executing test.js by "node test.js", a segmentation fault occurs when cFunc() calls cppFunc(). Segmentation fault is happening exactly at the line "Local c_back = Local::New(isolate, callback);" in cppFunc().

What could be the reason?

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.