1

何が間違っているのかわかりません。この関数の3番目の引数として渡すRubyハッシュがありTest::test()ます。

static VALUE nm_test(VALUE self, VALUE str, VALUE from, VALUE options) {
  if (TYPE(options) != T_HASH) {
    // raises error
  }

  if (rb_funcall(options, rb_intern("has_key?"), 1, rb_intern("dtype")) == Qtrue) { // SEGFAULT
    // success
  }

  // other stuff
}

void Init_test() {
  cTest = rb_define_module("Test");

  rb_define_singleton_method(cTest, "test", nm_test, 3);
}

一部のオブジェクトに存在しない関数を呼び出そうとすると、セグメンテーション違反が発生すると聞きました。また、オブジェクトが次のように応答することも確認しましたhas_key?

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?!");
}

これはエラーをトリガーしません。したがって、これは間違いなくハッシュであり、間違いなくありHash#has_key?ます。

これが実際のコードからの実際のバックトレースです(上記のコードではありません)。セグメンテーション違反が発生してい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

何か案は?

4

1 に答える 1

1

理解した。をrb_intern("whatever!")返すようIDで、rb_funcallには。が必要VALUEです。したがって、への呼び出しは次のrb_funcallようになります。

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

IDになるので混乱:has_key?しますが、問題なく機能しますが、引数自体は型である必要がありますVALUE

于 2012-08-23T20:29:06.757 に答える