node.js native addon – destructor of wrapped class doesn't run

I’m writing a node.js addon in C++. I wrap some class instances using node::ObjectWrap, to associate the native instance with a javascript object. My problem is, the wrapped instance’s destructor never runs.

Here is an example:

point.cc

#include <node.h>
#include <v8.h>

#include <iostream>

using namespace v8;
using namespace node;

class Point 
:ObjectWrap
{
protected:
    int x;
    int y;
public:
    Point(int x, int y) :x(x), y(y) {
        std::cout << "point constructs" << std::endl;
    }

    ~Point() {
        std::cout << "point destructs" << std::endl;
    }

    static Handle<Value> New(const Arguments &args){
        HandleScope scope;

        // arg check is omitted for brevity
        Point *point = new Point(args[0]->Int32Value(), args[1]->Int32Value());
        point->Wrap(args.This());

        return scope.Close(args.This());
    } 

    static void Initialize(Handle<Object> target){
        HandleScope scope;

        Local<FunctionTemplate> t = FunctionTemplate::New(New);
        t->InstanceTemplate()->SetInternalFieldCount(1);

        NODE_SET_PROTOTYPE_METHOD(t, "get", Point::get);

        target->Set(String::NewSymbol("Point"), t->GetFunction());
    } 

    static Handle<Value> get(const Arguments &args){
        HandleScope scope;
        Point *p = ObjectWrap::Unwrap<Point>(args.This());
        Local<Object> result =  Object::New();
        result->Set(v8::String::New("x"), v8::Integer::New(p->x));
        result->Set(v8::String::New("y"), v8::Integer::New(p->y));
        return scope.Close(result);
    } 
};

extern "C" void init(Handle<Object> target) {
    HandleScope scope;
    Point::Initialize(target);
};

test.js

var pointer = require('./build/default/point');

var p = new pointer.Point(1,2);
console.log(p.get());

I assume I have to set up an WeakPointerCallback, that deletes the manually allocated object, if the V8’s garbage collector wants it to. How am I supposed to do that?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Garbage collection is not guaranteed in V8 – it’s only executed when the engine is running out of memory. Unfortunately that means you’ll probably have to call a release function manually if you need to release resources – which would not be too great for the users of a JS API. (You can attach it to process.on(‘exit’) to make sure it gets called.)

Commentary on V8

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply