v8 WeakCallback 永远不会被调用 [英] v8 WeakCallback never gets called

查看:32
本文介绍了v8 WeakCallback 永远不会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题很旧,但我找到的所有答案都不起作用并且已经过时.但这是我的代码:

I know that this question is old but all the answers I found doesn't work and are outdated. But here is my code:

void Destroyed(const v8::WeakCallbackData<v8::Object, int>& info)
{
    std::cout << "d: " << *info.GetParameter() << std::endl;
}

std::vector<v8::Persistent<v8::Object, v8::CopyablePersistentTraits<v8::Object>>> persistent;
int i = 0;

void Constructor(const v8::FunctionCallbackInfo<v8::Value>& info)
{
    v8::HandleScope scope(v8::Isolate::GetCurrent());

    v8::Persistent<v8::Object, v8::CopyablePersistentTraits<v8::Object>> per;
    per = v8::Persistent<v8::Object>(v8::Isolate::GetCurrent(), info.This());

    per.SetWeak<int>(new int(i), Destroyed);

    persistent.push_back(per);

    ++i;

    while(!v8::V8::IdleNotification()) {}
}

void TestV8()
{
    v8::Isolate* isolate = v8::Isolate::New();

    v8::Isolate::Scope isolateScope(isolate);

    v8::HandleScope scope(isolate);

    v8::Local<v8::Context> context = v8::Context::New(isolate);

    v8::Context::Scope contextScope(context);

    v8::Local<v8::FunctionTemplate> temp = v8::FunctionTemplate::New(isolate, Constructor);

    context->Global()->Set(v8::String::NewFromUtf8(isolate, "Object"), temp->GetFunction());

    v8::Local<v8::String> source = v8::String::NewFromUtf8(isolate, "while(true) {new Object();}");

    v8::Local<v8::Script> script = v8::Script::Compile(source);

    v8::Local<v8::Value> result = script->Run();

    std::cout << *v8::String::Utf8Value(result) << std::endl;
}

如果我不将持久性添加到向量中,则内存不会增加.但是 Destroyed 永远不会被调用我似乎无法弄清楚出了什么问题,请帮忙 :)

If I don't add the persistent to the vector the memory doesn't increase. But Destroyed never gets called I can't seem to figure out what is wrong please help :)

推荐答案

我解决了下面是工作代码.显然,持久复制构造函数没有将它复制到弱引用处,因此将其转换为指针来解决它.

I solved it below is the working code. Apparently the Persistent copy constructor didn't copy that it where a weak reference so making it into pointers instead solved it.

std::vector<v8::Persistent<v8::Object, v8::CopyablePersistentTraits<v8::Object>>*> persistent;
int i = 0;

void Destroyed(const v8::WeakCallbackData<v8::Object, int>& info)
{
    std::cout << "d: " << *info.GetParameter() << std::endl;

    persistent[*info.GetParameter()]->Reset();
}

void Constructor(const v8::FunctionCallbackInfo<v8::Value>& info)
{
    v8::HandleScope scope(v8::Isolate::GetCurrent());

    v8::Persistent<v8::Object, v8::CopyablePersistentTraits<v8::Object>>* per = new v8::Persistent<v8::Object, v8::CopyablePersistentTraits<v8::Object>>(v8::Isolate::GetCurrent(), info.This());
    per->SetWeak<int>(new int(i), Destroyed);
    persistent.push_back(per);

    ++i;

    while(!v8::V8::IdleNotification()) {}
}

void TestV8()
{
    v8::Isolate* isolate = v8::Isolate::New();
    v8::Isolate::Scope isolateScope(isolate);

    v8::HandleScope scope(isolate);

    v8::Local<v8::Context> context = v8::Context::New(isolate);
    v8::Context::Scope contextScope(context);

    v8::Local<v8::FunctionTemplate> temp = v8::FunctionTemplate::New(isolate, Constructor);

    context->Global()->Set(v8::String::NewFromUtf8(isolate, "Object"), temp->GetFunction());

    v8::Local<v8::String> source = v8::String::NewFromUtf8(isolate, "while(true) {new Object();}");
    v8::Local<v8::Script> script = v8::Script::Compile(source);
    v8::Local<v8::Value> result = script->Run();

    std::cout << *v8::String::Utf8Value(result) << std::endl;
}

这篇关于v8 WeakCallback 永远不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆