Node.js addon object destruction

I am writing a GPU database and consider using javascript as a language for querying against using node.js.

I wrote the node addon since I wrote a GPU database in C ++. However, I have a problem with my node.js addon, because my C ++ objects are not destroyed, but only when I explicitly do not use the new operator. If I use a new operator, this is normal, just when calling a method that creates a new method - for example, copy (), etc. I use V8 :: AdjustAmountOfExternalAllocatedMemory (size ()) as an indication to V8 that I have allocated external memory (on the GPU).

Please, I can consult.

1. Code that correctly frees up GPU memory

This piece of code correctly frees the GPU memory by invoking the destructor of the object that makes the call to free the GPU memory:

var gpudb = require('./build/Release/gpudb');

var n = 1000000;
for (var i = 0; i < 10000; ++i) {
    var col = new gpudb.GpuArray(n);
}

2. However, this piece of code does not call the object destructor to free the GPU memory.

var gpudb = require('./build/Release/gpudb');

var n = 1000000;
var col = new gpudb.GpuArray(n);
for (var i = 0; i < 10000; ++i) {
        var copyOfCol = col.copy();
}

3. Now here are the functions for the constructor and the copy function, respectively.

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

  if (args.IsConstructCall()) {
    // Invoked as constructor: `new GpuVector(...)`
    int value = args[0]->IsUndefined() ? 0 : args[0]->NumberValue();
    GpuVector* obj = new GpuVector(value);
    obj->Wrap(args.This());
    return args.This();
  } else {
    // Invoked as plain function `GpuVector(...)`, turn into construct call.
    const int argc = 1;
    Local<Value> argv[argc] = { args[0] };
    return scope.Close(constructor->NewInstance(argc, argv));
  }
}

Handle<Value> GpuArray::Copy(const Arguments& args) {
    HandleScope scope;

    GpuArray* in = ObjectWrap::Unwrap<GpuVector>(args.This());
    GpuArray* out = new GpuArray(in); // creates new gpu memory slot and copies the data over

    out->Wrap(args.This());
    return args.This();
}
+3
source share
1 answer

Without the GpuArray constructor, it's a little harder to say what's wrong.

But I see some things that are wrong:

Handle<Value> GpuArray::Copy(const Arguments& args) {
    //...
    GpuArray* in = ObjectWrap::Unwrap<GpuVector>(args.This());

    //...
}

You do not clear the object GpuVectorfrom the object GpuArray, which is incorrect.

Wrap / Unwrap methods should be used to establish communication between C ++ objects and their respective js objects, and not between different objects.

, , (, , ), , , :

Handle<Value> GpuArray::Copy(const Arguments& args) {
    HandleScope scope;

    GpuArray* in = ObjectWrap::Unwrap<GpuArray>( args.This() );

    //build argc and argv here

    Local<Object> outJs = constructorOfGpuArray->NewInstance( argc, argv );
    GpuArray* out = ObjectWrap::Unwrap<GpuArray>( outJs );

    //set members/etc of the "out" object so it looks identical to the "in" object

    //return the js object to the caller.
    return scope.Close( outJs );
}

, .

0

All Articles