Okay I found the Buffer->GetContents() call for this use that doesn't
externalize it which is ideal for this temporary access for a callee.

PS: I still wonder, when having Externalize() how to know if V8 collected
already the Javascript side of the typed array, since when I simply
delete[] the data, things go bad, as the Uint32Array() handle still exists
in Javascript but points into bogus.

I guess even in cases when you want the data pointer around, you just hold
an persistent handle, get the contents for getContents() to it to stop V8
from collecting it and once your done, you let go of the persitent handle,
so the garbage collector will collect it.

So is there any safe use for Externalize() after all?


On Fri, Jan 29, 2016 at 9:39 AM, Axel Kittenberger <[email protected]> wrote:

> How does memory management of typed arrays in v8 work?
>
> I threw together below example and it works as expected, I can read and
> even alter the array from native code.
>
> However the v8 manual says, once I externalize an ArrayBuffer I am
> responsible for its memory management. But I don't know what it means and
> the docs regarding this are rather lightweight.
> So I suppose below example is a memory leak?
> Where would I have to delete or free() stuff?
>
> Kind regards, Axel
>
> --------------------------------------------
> var hello = require('./hello');
> var i32 = new Int32Array( [ 2, 3, 5, 7, 11, 13 ] );
> hello.test(i32);
>
> for( var i = 0, iZ = i32.length; i < iZ; i++ )
> {
> console.log( i, ': ', i32[ i ] );
> }
> --------------------------------------------
> #include <iostream>
> #include <nan.h>
>
> // just an example for a native library call taking an array of primitives.
> void native_func( int32_t * array )
> {
> for( size_t i = 0; i < 6; i++ )
> {
> std::cout << i << " " << array[ i ] << "\n";
> }
>
> array[ 0 ]++;
> }
>
> // wrapper for native_func for testing
> void test(const Nan::FunctionCallbackInfo<v8::Value>& info)
> {
> if( info.Length() != 1 )
> {
> Nan::ThrowError("array missing.");
> return;
> }
>
> if( !info[0]->IsInt32Array() )
> {
> Nan::ThrowError("not an Int32Array.");
> return;
> }
>
> v8::Local<v8::Int32Array> i32a = v8::Local<v8::Int32Array>::Cast( info[ 0
> ] );
>
> if( i32a->Length() < 6 )
> {
> Nan::ThrowError("array too short");
> return;
> }
>
> v8::Local<v8::ArrayBuffer> ab( i32a->Buffer() );
>
> v8::ArrayBuffer::Contents c( ab->Externalize() );
>
> native_func( static_cast<int32_t *>(c.Data() ) );
> }
>
> void init( v8::Local<v8::Object> exports )
> {
> exports->Set(
> Nan::New("test").ToLocalChecked(),
> Nan::New<v8::FunctionTemplate>(test)->GetFunction()
>    );
> }
>
> NODE_MODULE( addon, init )
> --------------------------------------------
>

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/CABg07fv3yWVrxOvS4p2TPPHso2V%3DSZ%2BGOdZkWrvARyJ%2BA1vCyA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to