Search code examples
c++v8persistent

v8 WeakCallback never gets called


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


Solution

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