Locally-scoped version of Persistent<T>?

101 views
Skip to first unread message

Marcel Laverdet

unread,
Apr 5, 2012, 9:32:43 PM4/5/12
to v8-u...@googlegroups.com
Hey I'm wondering why there isn't a helper class for Persistent<T> which will Dispose() a handle at the end of scope. It seems like right now v8 encourages lots of unfriendly cleanup code such as:

void function hello() {
  Persistent<Thing> thing = Persistent<Thing>::New(...);
  ...
  thing.Dispose();
}

This kind of code is difficult to maintain in many cases, and also vulnerable to memory leaks when using C++ exceptions. I'd like to see a version of Persistent<T> that behaves similarly to std::unique_ptr<T>. v8 already has helper classes like this with Isolate::Scope and Context::Scope.

Or perhaps there's a way to get what I want with local handles? I tried something like this to no avail:

Persistent<Context> pcontext = Context::New(NULL, global);
Local<Context> lcontext = pcontext;
pcontext.Dispose();

Any advise would be appreciated!

Vyacheslav Egorov

unread,
Apr 10, 2012, 3:50:19 AM4/10/12
to v8-u...@googlegroups.com
Locally scoped version of persistent is almost equivalent to Local
except that Local can never be weak.

> Local<Context> lcontext = pcontext;

It should be

Local<Context> lcontext = Local<Context>::New(pcontext);

--
Vyacheslav Egorov

> --
> v8-users mailing list
> v8-u...@googlegroups.com
> http://groups.google.com/group/v8-users

Marcel Laverdet

unread,
Apr 11, 2012, 9:23:42 PM4/11/12
to v8-u...@googlegroups.com
Ah that makes sense. Thank you!

Neha

unread,
Jan 22, 2013, 3:58:08 AM1/22/13
to v8-u...@googlegroups.com
 
 
Hi,
 
I have code as :
 

v8::Persistent <v8::Context> context = v8::Context::New( NULL, orderTemplate );

v8::Handle <v8::Script> script = v8::Script::Compile( source );

v8::Persistent <v8::Script > per_script = v8::Persistent<v8::Script>::New(script );

I store the persistent handle-> context and per_script. Later in my allication, i retrive the above and try to run the script as follows:

context->Enter();

per_script->Run();

context->Exit();

The above three statements try to retrive the object from the persistent handle and then run the script. This has a lot of overhead in terms of performance (Time taken to run the function) . How would this problem be solved?

 

 

 

Jakob Kummerow

unread,
Jan 22, 2013, 4:02:12 AM1/22/13
to v8-u...@googlegroups.com
Would you please stop sending the same question over and over again to this mailing list?

You're not giving enough detail for anyone to be able to help anyway. Repeating the question won't change that. Chances are your script just takes a lot of time to run. *shrug*

Neha

unread,
Jan 22, 2013, 6:04:16 AM1/22/13
to v8-u...@googlegroups.com
 
Hi,
 
Saw similar posts so asked the question there..
Sorry for the trouble..
 
When i comment the code for running the script, my code works fine. So does not look like a problem with the code.
 
I have pre-compiled and stored the context handle and script handle. later in my application, i get the context and try to run the script in the context. This is giving me a performance hit.
I have to run the script on a large number of object without the performance hit.
Reply all
Reply to author
Forward
0 new messages