Q:

Node.JS, C++ module: why I have segfault when I try to call Length method of Local?

When I call func() from js code and func() calls arr->Length() then I have segfault.
But somethimes I have strange random values in stdout: 283132217, -1222622919, -42974919, -112180935, 997212473, -1412415175.
Here's example:
#include <node.h>

using namespace v8;

Local<Array> arr = Array::New();

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

    printf("%d\n", arr->Length());

    return scope.Close(Undefined());
}

void init(Handle<Object> target)
{

    target->Set( String::NewSymbol("func"),
                 FunctionTemplate::New(func)->GetFunction() );

}

NODE_MODULE(ctest, init);

A:

I am not expert on v8, but I can solve problem like this:
Persistent<Array> arr = Persistent<Array>::New(Array::New());

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

    printf("%d\n", arr->Length());

    return scope.Close(Undefined());
}

Or put local handle to your function:
Handle<Value> func(const Arguments &args)
{
    HandleScope scope;
    Local<Array> arr = Array::New();

    printf("%d\n", arr->Length());

    return scope.Close(Undefined());
}

