I am trying to save JavaScript function in my c++ app and call it from another thread.
But I got "Unhandled exception at 0x0101B5D5 in Console.exe: 0xC0000005: Access violation reading location 0x00000017."
Saving the JavaScript function (in requestCallbacks vector):
Handle<Value> HttpEngine::addListener(const Arguments& args)
{
Locker locker;
HandleScope scope;
HttpEngine* pThis = UnwrapHttpEngine( args.This() );
Persistent<Function> callback = Persistent<Function>::New(Handle<Function>::Cast(args[0]));
pThis->requestCallbacks.push_back(*callback);
return Boolean::New(true);
}
An attempt to call it from another thread:
void HttpEngine::emit()
{
Locker locker;
HandleScope scope;
for (size_t i = 0; i < requestCallbacks.size(); i++)
{
Persistent<Function> func = static_cast<Function*>(requestCallbacks[i]);
Handle<Value> args[1];
args[0] = v8::String::New("http://google.com");
func->Call(Context::GetCurrent()->Global(), 1, args);
}
}
JavaScript code (where the httpEngine is my c++ object in global scope):
httpEngine.addListener(function (url) {
print('on request: ' + url);
});
What's the type of requestCallbacks
? Try putting the Persistent<Function>
in there, not a raw Function*
(using the *
operator on a Persistent
is both unsupported and most certainly not what you want to do).
User contributions licensed under CC BY-SA 3.0