I'm trouble about the following code:
void my_callback(result res, void *ctx) {
Napi::Promise::Deferred *deferred = (Napi::Promise::Deferred *)ctx;
Napi::Env env = deferred->Env(); // segmentation fault
if (res == RESULT_OK) {
deferred->Resolve(Napi::String::New(env, "Success"));
} else {
deferred->Reject(
Napi::Error::New(env, std::string("Failed")).Value());
}
}
Napi::Value MyClass::Do(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env);
do_async(my_callback, &deferred);
return deferred.Promise();
}
This code:
MyClass::Do is called by Nodejs code directly.
- After
do_async ends in another thread, my_callback begins.
my_callback sees res and executes deferred->Resolve or deferred->Reject.
I want to run this code as above but segmentation fault occurs in my_callback.
With the following restriction, How should I use deferred without errors in my_callback?
- Declaration about
my_callback cannot be changed.
FYI: That I tried in order to solve this problem.
I'm trouble about the following code:
This code:
MyClass::Dois called by Nodejs code directly.do_asyncends in another thread,my_callbackbegins.my_callbackseesresand executesdeferred->Resolveordeferred->Reject.I want to run this code as above but segmentation fault occurs in
my_callback.With the following restriction, How should I use
deferredwithout errors inmy_callback?my_callbackcannot be changed.FYI: That I tried in order to solve this problem.