I am writing a Node.js c++ addon which requires frequent callbacks from C++ to Javascript.
The constructor requires two functions, an success and an error callback.
...
// assuming info[0] and info[1] are functions with ->IsFunction()
MyClass* myClass = new MyClass();
myClass->Wrap(info.Holder());
myClass->onStateUpdateCallback = new Nan::Callback(info[0].As<v8::Function>());
myClass->onErrorCallback = new Nan::Callback(info[1].As<v8::Function>());
...
onStateUpdateCallback
and onErrorCallback
are both private properties of the class MyClass defined like this:
...
Nan::Callback* onStateUpdateCallback;
Nan::Callback* onErrorCallback;
...
Now when I call the onStateUpdateCallback
from another function it works the first time, but fails the second time by throwing EXC_BAD_ACCESS
(while debugging via lldb
).
Thats how I call the callback (Note: The function gets called from an external library that executes the calls in different threads, the first time in the main thread, the second time in thread #13, which may cause the problem!):
...
const int argc = 4;
// The arguments should be numbers and strings, but I simplified it to null...
v8::Local<v8::Value> argv[argc] = {
Nan::Null(),
Nan::Null(),
Nan::Null(),
Nan::Null()
};
onStateUpdateCallback->Call(argc, argv);
...
I assume that the function or the scope has been garbage collected by Javascript, but I have no idea how to prevent this behaviour, any ideas?
Thanks.
As I have thought, there was an issue with different threads.
I fixt the threading issue by using Nan::AsyncProgressWorker
.