Hiya,

I'm trying to perform finalization on sidecar data associated with a
v8 Function (the C++ API) but are having problems getting the GC
finalization callback (and/or a reference over-counting bug
somewhere).

Explanation of what I want do do in pseudo-ish code:

Function createFooFunction() {
  impl (args) { -- C++ function implementation -- }
  Foo *data = new Foo(); // some "arbitrary" data
  return Function(&impl, data);
}

When a Function created using this method is no longer referenced I
want to clean up "data". I've understood that a v8 Persistent handle
is the way to go, but after implementing 3 different approaches and
the "weak callback" still nog getting called (even though I'm
constantly draining V8::IdleNotification), I'm getting a bit hopeless/
desperate.

This is the most minimal implementation which should work (according
to the v8 api guide):

using namespace v8;

static Handle<Value> Handler(const Arguments& args) {
  fprintf(stderr, "Handler\n"); fflush(stderr);
  return Undefined();
}

static void WeakCallback(Persistent<Value> value, void *data) {
  fprintf(stderr, "WeakCallback\n"); fflush(stderr);
}

Local<Function> Create() {
  HandleScope scope;
  void *data = NULL; // points to something in reality
  Local<FunctionTemplate> t =
      FunctionTemplate::New(&Handler, External::Wrap(data));
  Local<Function> fun = t->GetFunction();
  Persistent<Function> phandle = Persistent<Function>::New(fun);
  phandle.MakeWeak(data, &WeakCallback);
  return scope.Close(fun);
}

~~~

Now the code which call Create:

void createAndCallAFunction() {
  HandleScope scope;
  Local<Function> fun = Create();
  fun->Call(Context::GetCurrent()->Global(), 0, NULL);
}

...
createAndCallAFunction();
while (V8::IdleNotification()) {};
...

The "Handler" gets called as expected, but "WeakCallback" is never
called.

Any ideas?

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

Reply via email to