Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么使用rb_funcall调用Hash#has_key时会出现segfault?_C_Ruby - Fatal编程技术网

为什么使用rb_funcall调用Hash#has_key时会出现segfault?

为什么使用rb_funcall调用Hash#has_key时会出现segfault?,c,ruby,C,Ruby,我不确定我做错了什么。我有一个Ruby散列,作为这个函数的第三个参数,Test::Test() 我听说如果你试图调用某个对象上不存在的函数,你会得到一个segfault。我还检查了对象是否响应具有\u键?: if (TYPE(options) != T_HASH) { rb_raise(rb_eArgError, "third argument to repack must be hash"); } else if (!rb_respond_to(options, rb_intern("ha

我不确定我做错了什么。我有一个Ruby散列,作为这个函数的第三个参数,
Test::Test()

我听说如果你试图调用某个对象上不存在的函数,你会得到一个segfault。我还检查了对象是否响应
具有\u键?

if (TYPE(options) != T_HASH) {
  rb_raise(rb_eArgError, "third argument to repack must be hash");
} else if (!rb_respond_to(options, rb_intern("has_key?"))) {
  rb_raise(rb_eArgError, "hash does not respond to has_key?!");
}
这不会触发错误。所以它肯定是一个散列,并且肯定有
散列#有_key?

这里是实际代码的实际回溯(不是上面的代码)。SEGFULT位于
rb_funcall
线路上

Program received signal SIGSEGV, Segmentation fault.
rb_type (obj=44840) at ./include/ruby/ruby.h:1344
1344    ./include/ruby/ruby.h: No such file or directory.
(gdb) bt
#0  rb_type (obj=44840) at ./include/ruby/ruby.h:1344
#1  rb_any_hash (a=44840) at hash.c:83
#2  0x080fc570 in st_lookup (table=0x8fa0470, key=44840, value=0x0) at st.c:341
#3  0x08066ef8 in rb_hash_has_key (hash=144171660, key=44840) at hash.c:1516
#4  0x08157f7d in vm_call0 (th=0x8265b88, recv=144171660, id=5127, argc=1, argv=0xbfffdf60, me=0x82ce480)
    at vm_eval.c:79
#5  0x081587c4 in rb_call (scope=CALL_FCALL, argv=0xbfffdf60, argc=1, mid=5127, recv=144171660) at vm_eval.c:456
#6  rb_funcall (recv=144171660, mid=5127, n=1) at vm_eval.c:658
#7  0xb70933f8 in nm_rbstring_matlab_repack (self=142142600, str=144171680, from=12992526, options=144171660)
    at ../../../../ext/nmatrix/util/io.cpp:210

有什么想法吗?

想出来了。似乎
rb\u intern(“随便什么!”)
返回一个
ID
,rb\u funcall需要一个
值。因此对
rb_funcall
的调用应该如下所示:

rb_funcall(options, rb_intern("has_key?"), 1, ID2SYM(rb_intern("dtype")));

这很令人困惑,因为
:has_key?
必须是一个ID,并且工作正常——但是参数本身必须是
VALUE
类型。似乎
rb\u intern(“随便什么!”)
返回一个
ID
,rb\u funcall需要一个
值。因此对
rb_funcall
的调用应该如下所示:

rb_funcall(options, rb_intern("has_key?"), 1, ID2SYM(rb_intern("dtype")));

这很令人困惑,因为
:has_key?
必须是一个ID,并且工作正常——但是参数本身必须是
VALUE

类型,谢谢,抱歉。这是实际代码的一部分:--想让人们更容易阅读。哦,segfault在要点的第31行。谢谢,对不起。这是实际代码的一部分:--希望让人们更容易阅读。哦,segfault在要点的第31行。