ObjectTemplate::NewInstance causes memory access error

222 views
Skip to first unread message

HdcDst

unread,
Jul 6, 2011, 10:09:17 AM7/6/11
to v8-users
I am using the following C code in my Program:

1: HandleScope handle_scope;
2: Handle<ObjectTemplate> global = ObjectTemplate::New();
3: Handle<ObjectTemplate> point_templ = ObjectTemplate::New();
4: point_templ->SetInternalFieldCount(1);
5: point_templ->SetAccessor(String::New("x"), Point::GetPointX,
6: Point::SetPointX);
7: point_templ->SetAccessor(String::New("y"), Point::GetPointY,
8: Point::SetPointY);
9: Point *p = new Point(3,4);
10: Local<Object> obj = point_templ->NewInstance();
11: obj->SetInternalField(0, External::New(p));

But when at line 10: "Local<Object> obj = point_templ-
>NewInstance();"
The Program stops with a memory access error.
Does anyone has an Idea, what's wrong in my code?

Matthias Ernst

unread,
Jul 6, 2011, 10:23:00 AM7/6/11
to v8-u...@googlegroups.com
> --
> v8-users mailing list
> v8-u...@googlegroups.com
> http://groups.google.com/group/v8-users

Some relevant context is missing from your example.
So I can only guess: are you executing within a Context::Scope?
What is the point of "global"? It smells as if you meant to
instantiate a context.

You may want to build against the v8 debug version; it may give better
errors (http://code.google.com/apis/v8/build.html).

Matthias

Stephan Beal

unread,
Jul 6, 2011, 10:16:23 AM7/6/11
to v8-u...@googlegroups.com
On Wed, Jul 6, 2011 at 4:09 PM, HdcDst <brai...@a1.net> wrote:
...
 
11:     obj->SetInternalField(0, External::New(p));

But when at line 10: "Local<Object> obj = point_templ-
>NewInstance();"
The Program stops with a memory access error.
Does anyone has an Idea, what's wrong in my code?

i don't know if this is the problem, but i have never used ObjectTemplate::NewInstance(), but Function::NewInstance() (using the constructor Function). That has always worked well for me as long as i remember to use Arguments::IsConstructorCall() in the ctor impl to avoid a assertion/crash case when the user forgets the 'new' operator.

:-?

This _might_ be helpful as a studying point:


--
----- stephan beal
http://wanderinghorse.net/home/stephan/

HdcDst

unread,
Jul 6, 2011, 1:32:18 PM7/6/11
to v8-users


On Jul 6, 4:23 pm, Matthias Ernst <matth...@mernst.org> wrote:
This helped.
The problem was, that I tried to instantiat a template before I set up
a context scope.
Setting up a context, getting the global object instead the global
template, and then setting
the point_object as a property of the global object worked.
-- HdcDst

Stephan Beal

unread,
Jul 6, 2011, 1:46:06 PM7/6/11
to v8-u...@googlegroups.com
On Wed, Jul 6, 2011 at 7:32 PM, HdcDst <brai...@a1.net> wrote:
The problem was, that I tried to instantiat a template before I set up
a context scope.
Setting up a context, getting the global object instead the global
template, and then setting
the point_object as a property of the global object worked

Incidentally... i recently wrote a class which handles this setup for you, specifically because i could never remember the proper setup ordering (almost any setup order compiles fine but all but the correct ordering assert at runtime). This _might_ be useful for you:

Short demo:
The code:

that class is _almost_ "standalone" - its only dependencies are v8 and the STL except for two internal uses of a StringBuffer class (which works like a std::ostringstream but also handles v8 types). i'll make a note of factoring out that dep so that others can steal that class for use independent of the lib it lives in.

Stephan Beal

unread,
Jul 6, 2011, 1:50:56 PM7/6/11
to v8-u...@googlegroups.com
On Wed, Jul 6, 2011 at 7:46 PM, Stephan Beal <sgb...@googlemail.com> wrote:
that class is _almost_ "standalone" - its only dependencies are v8 and the STL except for two internal uses of a StringBuffer class (which works like a std::ostringstream but also handles v8 types). i'll make a note of factoring out that dep so that others can steal that class for use independent of the lib it lives in.

[stephan@cheyenne:~/cvs/v8-juice/convert]$ svn ci -m 'Removed the StringBuffer dep so that V8Shell can be stolen for use in arbitrary clients.' include/v8/convert/V8Shell.hpp
Sending        include/v8/convert/V8Shell.hpp
Transmitting file data .
Committed revision 1769.

Happy Hacking!
 
Reply all
Reply to author
Forward
0 new messages