On Sun, Apr 3, 2011 at 6:29 AM, developerdr <[email protected]> wrote:
> Dear Mads,
>
> thank you very much for your answer. Please find the simple example
> below (it took me some time to make it that small).
> The problem occurs even without the usage of additional threads.
> I think it's obviously that my code must have a logical mistake -
> because it's that simple.
> However, according to all the explanations provided in this mailing
> list and provided by the tutorials on the developer side I'm not able
> to find the problem...
>
> The example is written for windows and you should be able to test it
> using a simple console application.
> You will see that the crash occurs during the second context creation
> using the one and only static but persistent object template.
> As I understand the documentation of persistent objects: they are not
> influenced by the handle scope and thus the example should work fine.
> If you replace the usage of the static object template by two
> independent objects template the example does not crash...
>
> Thanks for you support,
> Jan
>
> #include <tchar.h>
> #include <cstdlib>
> #include <iostream>
> #include <cassert>
> #include <v8.h>
> #include <windows.h>
>
> /**
>  * Simple singleton implementation.
>  */
> class Singleton
> {
>        public:
>
>                /**
>                 * Returns the unique singleton object.
>                 */
>                static Singleton& get()
>                {
>                        static Singleton singleton;
>                        return singleton;
>                }
>
>                /**
>                 * Returns the unique object template.
>                 */
>                v8::Persistent<v8::ObjectTemplate> objectTemplate()
>                {
>                        if (uniqueObjectTemplate.IsEmpty())
>                        {
>                                uniqueObjectTemplate =
> v8::Persistent<v8::ObjectTemplate>(v8::ObjectTemplate::New());

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;
> }
>
> --
> v8-users mailing list
> [email protected]
> http://groups.google.com/group/v8-users

-- 
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users

Reply via email to