gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:55:19 24 4
gpt4 key购买 nike

当我从 js 代码调用 func() 并且 func() 调用 arr->Length() 然后我有 段错误

但有时我在 stdout 中有奇怪的随机值:283132217、-1222622919、-42974919、-112180935、997212473、-1412415175。

例子如下:

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

最佳答案

我不是 v8 专家,但我可以解决这样的问题:

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

或者将本地句柄放入您的函数:

Handle<Value> func(const Arguments &args)
{
HandleScope scope;
Local<Array> arr = Array::New();

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

return scope.Close(Undefined());
}

关于c++ - Node.JS,C++ 模块 : why I have segfault when I try to call Length method of Local<Array>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20858935/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com