2014-02-26 81 views
3

我正在寫一個GPU數據庫,並且使用javascript作爲語言來查詢使用node.js.Node.js插件對象破壞

我一直在編寫節點插件,因爲我已經用C++編寫了GPU數據庫。不過,我的node.js插件有問題,因爲我的C++對象沒有被破壞,只是當我沒有明確使用new運算符時。如果我正在使用new運算符,那很好,它只是在調用一個創建新方法的方法時 - 比如copy()等。我正在使用V8 :: AdjustAmountOfExternalAllocatedMemory(size())作爲V8的指示,分配的外部存儲器(在GPU上)。

請給我一些建議。

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

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

1.一種可正確釋放GPU存儲器

代碼本的代碼正確地通過調用對象的析構函數,這使得呼叫以釋放GPU存儲器釋放GPU存儲器2.但是,這段代碼不會調用對象的析構函數來釋放GPU內存。

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.現在,這裏分別構造函數和複製功能的功能。

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(); 
} 

回答

0

沒有GpuArray構造這是一個有點難以分不清什麼是錯的。

但我可以看到一些東西,是不正確的:

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

    //... 
} 

您unwraping一個GpuVector對象,從GpuArray對象,這是錯誤的。

應該使用Wrap/Unwrap方法在C++對象和它們各自的js對象之間建立連接,而不是在不同的對象之間建立連接。

從您發佈的代碼,它看起來像一樣,你正試圖克隆對象,如果我是正確的,它應該做這樣的(但我可能是錯了):

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); 
} 

我沒有測試代碼,但它理論上應該工作