if you store and reuse your global object template you definitely have
to put it in a persistent handle. Otherwise it will be garbage
collected when you exit the handle scope in which the handle to it was
created and further uses of it will lead to crashes.
If you could create a simple reproduction that we can compile and run
to experience the problem that would make it much easier to help. We
have not noticed any such problems.
Thanks, -- Mads
> --
> v8-users mailing list
> v8-u...@googlegroups.com
> http://groups.google.com/group/v8-users
>
if you store and reuse your global object template you definitely have
to put it in a persistent handle. Otherwise it will be garbage
collected when you exit the handle scope in which the handle to it was
created and further uses of it will lead to crashes.
No, not if you do not allocate a persistent handle and register a weak
callback. The JS memory will be free for allocation as soon as the
handle exits its handle scope. We are talking about an ObjectTemplate
which is in the V8 heap with no associated C++ allocated data.
-- Mads
Here's your problem. You're missing a "::New" and instead you're
casting a local to a persistent handle:
/**
* "Casts" a plain handle which is known to be a persistent handle
* to a persistent handle.
*/
template <class S> explicit inline Persistent(Handle<S> that)
: Handle<T>(*that) { }
Maybe V8 team could add an overload Persistent(Local) that triggers a
compile-time error, or at least a useful runtime error.
Matthias
>
> // normally here function templates would be added
> //uniqueObjectTemplate->Set(String::New("functionName"),
> createSpecificFunctionTemplate())
> }
>
> return uniqueObjectTemplate;
> }
>
> private:
>
> v8::Persistent<v8::ObjectTemplate> uniqueObjectTemplate;
> };
>
> int _tmain(int argc, _TCHAR* argv[])
> {
> {
> v8::HandleScope handleScope;
>
> {
> v8::Persistent<v8::Context> anyContext = v8::Context::New(NULL,
> Singleton::get().objectTemplate());
>
> // do something with this context
>
> anyContext.Dispose();
> }
> }
>
> {
> v8::HandleScope handleScope;
>
> {
> // ** CRASH ->
> v8::Persistent<v8::Context> anyContext = v8::Context::New(NULL,
> Singleton::get().objectTemplate());
> // <-- CRASH **
>
> // do something with this context
>
> anyContext.Dispose();
> }
> }
>
> std::cout << "Press a key to exit" << std::endl;
> getchar();
>
> return 0;
> }
>