我最近使用v8(google的javascript引擎)。当GC运行时,String :: New总是在ASSERT(state_!= NEAR_DEATH)(global-handles.cc 237line)触发断言。有一些建议。
以下是我的一些源代码段:
void javascript_ctx_impl::call_obj_func (v8::persistent<object> object, const char* method, int argc, handle<value> argv[])
{
handlescope handle_scope;
local<value> cb = object->get(string::new(method));
if (!cb->isfunction()) {
std::cerr << "method = " << method << std::endl;
return;
}
local<function> do_action = local<function>::cast(cb);
trycatch try_catch;
/**ASSERT HERE **/
**do_action->call(object, argc, argv);**
/**ASSERT HERE **/
if (try_catch.hascaught()) {
v8::local<v8::message> msg = try_catch.message ();
if (!msg->getscriptresourcename().isempty() && !msg-
>getscriptresourcename()->isundefined())
{
v8::string::asciivalue name (msg-
>getscriptresourcename());
std::cerr << *name << std::endl;
}
else {
std::cerr << "call_obj_func: runtime error." << std::endl;
}
}
}
template <typename T>
class write_handle : public handle_impl_base
{
public:
write_handle (boost::asio::io_service& io, v8::Persistent<Object>
local,v8::Persistent<Object> h)
: handle_impl_base (io), handle_ (h), session_ (local)
{
}
public:
void operator () (const boost::system::error_code& ec,
std::size_t bytes_transferred)
{
HandleScope handle_scope;
if (!ec) {
Handle<Value> args[3] = {
js::instance ().safe_new_value (session_),
js::instance ().safe_new_value ("TRUE"),
js::instance ().safe_new_value (bytes_transferred)
};
js::instance ().call_obj_func (handle_, "onHandle", 3, args);
}
else {
Handle<Value> args[3] = {
js::instance ().safe_new_value (session_),
js::instance ().safe_new_value ("FALSE"),
js::instance ().safe_new_value (bytes_transferred)
};
js::instance ().call_obj_func (handle_, "onHandle", 3, args);
}
handle_.Dispose (); session_.Dispose ();
}
static void handle_weak (Persistent<Value> object, void*
parameter)
{
object.Dispose ();
}
private:
v8::Persistent<Object> handle_;
v8::Persistent<Object> session_;
};
v8::Handle<v8::Value> js_asio_socket_ip_tcp_function::async_write (const v8::Arguments& args)
{
HandleScope hScope;
js_asio_socket_ip_tcp_function* native_obj =
unwrap<js_asio_socket_ip_tcp_function>(args.This());
if (args.Length () < 4) {
return ThrowException (Exception::TypeError(String::New(
"async_resolve need 4 parameters."))
);
}
/** Argument check here */
js_stream_function* s = unwrap<js_stream_function> (args[1]-
>ToObject ());
if (s == NULL) {
return ThrowException (Exception::TypeError(String::New(
"async_resolve parameter 2 error."))
);
}
v8::Local<v8::Object> p0 = args[0]->ToObject ();
v8::Local<v8::Integer> p2 = args[2]->ToUint32 ();
v8::Persistent<Object> handle;
v8::Persistent<Object> sessin;
if (args[3]->ToObject ()->IsFunction ()) {
v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast(args[3]->ToObject());
handle = v8::Persistent<v8::Object>::New(f);
}
else {
handle = v8::Persistent<Object>::New (args[3]->ToObject ());
}
handle.MakeWeak (NULL, write_handle<void>::handle_weak);
handle.MarkIndependent ();
sessin = v8::Persistent<Object>::New (p0);
boost::asio::async_write (*(native_obj->socket_),
boost::asio::buffer (s->get (), p2->Value ()),
boost::asio::transfer_all (),
make_concrete_handle (write_handle <void> (native_obj-
>socket_->get_io_service (), sessin, handle)
)
);
return v8::Undefined ();
}
答案 0 :(得分:1)
您的write_handle<T>::handle_weak
为空。它应该从不为空,因为这违反了https://github.com/v8/v8/blob/master/include/v8.h#L124-132