I am using v8 (tag 3.22.19) built as shared library, the environment is windows 8.1 x64, visual studio 2010.
I use External::new(ptr) to pass the pointer to Print function, but the ptr is allocated use new, I don't know how to free the memory.
Then i came across the API SetWeak, but there is no document at all. I try to use it with the codes below, but the WeakPtrCallback is never called, which leads to the memory leak.
#include <v8.h>
#include <vld.h>
#include <memory>
void Print(const v8::FunctionCallbackInfo<v8::Value>& data) {
}
void WeakPtrCallback(
const v8::WeakCallbackData<v8::Object, std::weak_ptr<int>>& data) {
delete data.GetParameter();
}
int main() {
auto isolate = v8::Isolate::GetCurrent();
v8::HandleScope handle_scope(isolate);
auto context = v8::Context::New(isolate);
auto ptr = std::shared_ptr<int>(new int(3));
auto data = new std::weak_ptr<int>(ptr);
auto global = context->Global();
auto func_tpl = v8::FunctionTemplate::New(Print, v8::External::New(data));
context->Enter();
auto func = func_tpl->GetFunction();
global->Set(v8::String::New("print"), func);
auto obj = v8::Persistent<v8::Object>(isolate, global);
obj.SetWeak<std::weak_ptr<int>>(data, WeakPtrCallback);
context->Exit();
obj.Reset();
return 0;
}