v8 WeakCallback never gets called

2019-07-18 10:56发布

问题:

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

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 :)

回答1:

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


回答2:

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