Thanks Mike M! My solution above does seem to currently work, but the fact
that Sven states this won't be guaranteed to work in the future is
disappointing. I expect the javascript objects in the heap that reference
my C++ objects to be cleaned up when I quit my application. In doing so, I
want my C++ objects freed. To me this seems like basic resource accounting
that I expect v8 to support.
Mike, here's my latest code that does free my C++ objects on termination:
{
v8::V8::Initialize();
// 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();
point_proto->Set("method_a", FunctionTemplate::New(PointMethod_A));
point_proto->Set("method_b", FunctionTemplate::New(PointMethod_B));
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);
DVA_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();
DVA_ASSERT(3 == resultValue32);
}
// Dispose the persistent context.
contextP->Dispose(isolateP);
contextP->Clear();
contextScopeP.reset();
contextP.reset();
}
v8::V8::LowMemoryNotification();
v8::V8::Dispose();
#ifdef BEAKER_DEBUG_V8_ALLOCATIONS
CheckV8AllocCountIsZero();
#endif
On Wed, Jun 12, 2013 at 9:08 AM, Mike Moening <[email protected]>wrote:
> Sven,
>
> This problem keeps biting embedders over and over and over.
> Even if Chrome does not need it...
> The rest of the world needs a way to GUARANTEE that C++ backed JS objects
> get destroyed in a reliable and predicable manner.
>
> There are many hacks people use to try to get weak callbacks to fire. This
> is just another attempt.
>
> Can you or anyone propose a mechansim for fixing this?
> I would be happy to attempt implementation if a decent proposal would come
> forward for:
>
> 1) Guaranteeing weak callbacks for all objects fire on shutdown of V8.
> (necessary for proper memory leak testing)
> 2) A mechansim for telling V8 to collect garbage and fire weak callbacks
> on demand.
>
> If we don't fix this right, everyone and their uncle will be using this
> "solution"...
> We have no other choice.
>
> Mike M.
>
> --
> --
> v8-users mailing list
> [email protected]
> http://groups.google.com/group/v8-users
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "v8-users" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/v8-users/ta9wkdEY08o/unsubscribe?hl=en.
> To unsubscribe from this group and all its topics, send an email to
> [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
--
--
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.