How to trace the object lifecycle and free resource when it was collected

98 views
Skip to first unread message

Flier Lu

unread,
Dec 15, 2010, 12:39:46 PM12/15/10
to v8-users
I need to store some C pointers in a v8 object, which may point to a
data area. So, I use External::New to convert a C++ object pointer and
Object::SetHiddenValue to store it, but I'm not sure how to trace its
lifecycle. It means, when the Javascript object was collected, I need
be notified and delete the C++ object that was bound to the Javascript
object.

Are there any hook or callback could do it?

Thanks

Stephan Beal

unread,
Dec 15, 2010, 12:56:28 PM12/15/10
to v8-u...@googlegroups.com
On Wed, Dec 15, 2010 at 6:39 PM, Flier Lu <flier.lu@gmail.com> wrote:
I need to store some C pointers in a v8 object, which may point to a
data area. So, I use External::New to convert a C++ object pointer and
...

Are there any hook or callback could do it?

What you're looking for is called a Persistent Handle (or Weak Persistent Handle):


Those docs will get you started, but getting used to them requires some experimentation and/or poking around other people's code.

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

Matthias Ernst

unread,
Dec 15, 2010, 1:14:50 PM12/15/10
to v8-u...@googlegroups.com
node.js' ObjectWrap might be an inspiration for you:
https://github.com/ry/node/blob/master/src/node_object_wrap.h

On Wed, Dec 15, 2010 at 6:56 PM, Stephan Beal <sgb...@googlemail.com> wrote:

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

Flier Lu

unread,
Dec 17, 2010, 10:29:12 AM12/17/10
to v8-users
Thanks for your suggestion.

In fact, I have tried to use it in other scene, but it crash in some
situation, and I can't find the root cause :(

The broken code like this

v8::Persistent<v8::External>::New(payload).MakeWeak(payload->Value(),
DisposeCallback);

Anyway, I will try again

On 12月16日, 上午1时56分, Stephan Beal <sgb...@googlemail.com> wrote:

Flier Lu

unread,
Dec 17, 2010, 12:18:43 PM12/17/10
to v8-users
Thanks, the problem has been solved

I found the root cause, because I'm tracing the external handle
instead the object itself, which cause the crash :(

On 12月16日, 上午2时14分, Matthias Ernst <matth...@mernst.org> wrote:
> node.js' ObjectWrap might be an inspiration for you:https://github.com/ry/node/blob/master/src/node_object_wrap.h
>
>
>
>
>
>
>
> On Wed, Dec 15, 2010 at 6:56 PM, Stephan Beal <sgb...@googlemail.com> wrote:
Reply all
Reply to author
Forward
0 new messages