v8 WeakCallback never gets called

纵饮孤独 提交于 2019-12-06 05:37:43

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;
}

Have you checked the documentation of SetWeak? It clearly states the following:

/**
 *  Install a finalization callback on this object.
 *  NOTE: There is no guarantee as to *when* or even *if* the callback is
 *  invoked. The invocation is performed solely on a best effort basis.
 *  As always, GC-based finalization should *not* be relied upon for any
 *  critical form of resource management!
 */
template<typename P>
V8_INLINE void SetWeak(
    P* parameter,
    typename WeakCallbackData<T, P>::Callback callback);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!