On Thu, Oct 6, 2011 at 11:15 AM, Ondřej Žára <[email protected]> wrote:

>
> 2011/10/6 Stephan Beal <[email protected]>
>
>> On Thu, Oct 6, 2011 at 4:30 PM, ondras <[email protected]> wrote:
>>
>>> another question: suppose my JS object "A" (implemented in C++) needs a
>>> (hidden) reference to another JS object "B". I store the reference using
>>>
>>> A->SetInternalField(0, v8::Persistent<v8::Object>::New(B));
>>>
>>
>> If i'm not sorely mistaken, you do not need a Persistent handle for that
>> purpose. If B is-a Native and was created from JS code then it will already
>> have a Persistent handle (somewhere), and a non-persistent Handle to it is
>> the same as having a reference to it in JS (if i'm not sorely mistaken).
>>
>>
> So you suggest storing a v8::Local in internal field, instead of
> Persistent? That does not make much sense to me: local handles are
> stack-allocated and this will crash badly once the HandleScope is
> destroyed...
>

The Handle you pass in is not stored.  In fact, looking at the source for
SetInternalField, the handle you pass in is dereferenced into the raw
Object* and that is stored instead.

So doing:

A->SetInternalField(0, v8::Persistent<v8::Object>::New(B));

is actually leaking a persistent handle to B, since SetInternalField is not
holding onto the handle at all and there is no way to get that specific
handle back.

Another clue about this is that GetInternalField returns a Local, so it's
actually generating a new handle every time you look up the value, rather
than returning exactly the Handle you originally passed in.

matt

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

Reply via email to