You need to allocate a Persistent handle to the JS instance and make
it weak. In the weak callback you can deallocate your C heap data.

http://stackoverflow.com/questions/173366/how-do-you-free-a-wrapped-c-object-when-associated-javascript-object-is-garbage


On Tue, Jun 29, 2010 at 5:49 AM, quotient7 <[email protected]> wrote:
> Hi. I'm very new to v8 - and I love it!
> I want to create a system in which the "user" can load scripts, each
> script should have an own context.
> Also, they should be able to create "instances" (if they exist in
> JavaScript so far) through wrapper functions.
> Pseudo code should be:
>
> var myVar = Variables.create("blah");
> myVar.set("x");
>
> My current implementation looks like this:
>
> Handle<Value> CVariables::v8_Create(const Arguments &args)
> {
>        // TODO: Check arguments
>        String::AsciiValue ascName(args[0]);
>
>        // Create the instance for the variable
>        CVariable *pVariable = pVariables->Create(*ascName);
>
>        if (!pVariable)
>        {
>                // TODO: Throw Exception
>                return Null();
>        }
>
>        // Create the JS "instance" for the Variable object
>        Handle<Function> Constructor = pVariables->m_Variable->GetFunction();
>        Handle<Object> Instance = Constructor->NewInstance();
>        Instance->SetInternalField(0,
> Local<Value>(External::New(pVariable)));
>
>        return Instance;
> }
>
> This works well, I can call it. But the problem is, that it seems that
> pVariable won't get deallocated. Is it possible to automaticly
> deallocate when the GC of v8 deallocates the "instance" of the
> Variable object?
>
> Thanks for any help :)
>
> --
> 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