Like a good C++ programmer, I'm trying to use shared_ptrs to manage the lifetime of my C++ objects. I would expect this code below in testv8() to invoke my ~Point destructor, but it's never called! Can anyone tell me why?
v8::Handle<v8::Value> GetPointX(v8::Local<v8::String> property,
const v8::AccessorInfo &info) {
using namespace v8;
Local<Object> self = info.Holder();
Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
void* ptr = wrap->Value();
int value = static_cast<Point*>(ptr)->x_;
return Integer::New(value);
}
void SetPointX(v8::Local<v8::String> property, v8::Local<v8::Value> value,
const v8::AccessorInfo& info) {
using namespace v8;
Local<Object> self = info.Holder();
Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
void* ptr = wrap->Value();
static_cast<Point*>(ptr)->x_ = value->Int32Value();
}
{
// Get the default Isolate created at startup.
v8::Isolate* isolateP = v8::Isolate::GetCurrent();
// Create a stack-allocated handle scope.
v8::HandleScope handle_scope(isolateP);
// Create a template for the global object where we set the
// built-in global functions.
v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
// Create a new context.
std::shared_ptr<v8::Persistent<v8::Context> > contextP(new v8::Persistent<v8::Context>(v8::Context::New(NULL, global)));
std::shared_ptr<v8::Context::Scope> contextScopeP(new v8::Context::Scope(*contextP));
{
using namespace v8;
Handle<FunctionTemplate> point_templ = FunctionTemplate::New();
point_templ->SetClassName(String::New("Point"));
Handle<ObjectTemplate> point_proto = point_templ->PrototypeTemplate();
Handle<ObjectTemplate> point_inst = point_templ->InstanceTemplate();
point_inst->SetInternalFieldCount(1);
point_inst->SetAccessor(String::New("x"), GetPointX, SetPointX);
Handle<Function> point_ctor = point_templ->GetFunction();
std::shared_ptr<Point> ptP(new Point(0, 0));
v8::Persistent<v8::Object> obj = wrapSharedPtr(point_ctor, ptP);
assert(obj.IsWeak(isolateP));
(*contextP)->Global()->Set(String::New("point"), obj);
obj.Dispose(isolateP);
Handle<String> source = String::New("point.x = 3;");
Handle<Script> script = Script::Compile(source);
Handle<Value> result = script->Run();
source = String::New("point.x;");
script = Script::Compile(source);
result = script->Run();
std::int32_t resultValue32 = result->Int32Value();
assert(3 == resultValue32);
}
// Dispose the persistent context.
contextP->Dispose(isolateP);
contextP->Clear();
contextScopeP.reset();
contextP.reset();
}
v8::V8::LowMemoryNotification(); // I saw this mentioned as a way to force GC, but it doesn't help. Even so, I would think v8::V8::Dispose would clear all garbage.