gpt4 book ai didi

c - Ruby C 扩展 : rb_funcall causing segfault

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

我的扩展使用 Ruby 库通过接口(interface)发送和接收命令和遥测数据包,并将它们提供给 C 文件传输库进行处理。

收到大量数据包后,我会遇到此段错误。这是 GDB 的回溯。

            #0  search_method (defined_class_ptr=<synthetic pointer>, id=6177, klass=<optimized out>) at vm_method.c:567
#1 rb_method_entry_get_without_cache (klass=19934160, id=6177, defined_class_ptr=0x12c5500) at vm_method.c:592
#2 0x00007ffff7aced90 in rb_method_entry (klass=klass@entry=19934160, id=<optimized out>,
defined_class_ptr=defined_class_ptr@entry=0x12c5500) at vm_method.c:663
#3 0x00007ffff7acee71 in vm_search_method (ci=ci@entry=0x12c54c0, recv=<optimized out>) at vm_insnhelper.c:842
#4 0x00007ffff7ad4b3d in vm_exec_core (th=th@entry=0x6035d0, initial=initial@entry=0) at insns.def:1068
#5 0x00007ffff7ad951b in vm_exec (th=th@entry=0x6035d0) at vm.c:1440
#6 0x00007ffff7adac91 in vm_call0_body (th=th@entry=0x6035d0, ci=ci@entry=0x7fffffffbcb0, argv=0x7fffffffbd60)
at vm_eval.c:180
#7 0x00007ffff7adb6c6 in vm_call0 (defined_class=<optimized out>, me=<optimized out>, argv=<optimized out>,
argc=1, id=14481, recv=22956880, th=0x6035d0) at vm_eval.c:59
#8 rb_call0 (recv=recv@entry=22956880, mid=14481, argc=argc@entry=1, argv=argv@entry=0x7fffffffbd60,
scope=scope@entry=CALL_FCALL, self=<optimized out>) at vm_eval.c:349
#9 0x00007ffff7adbc64 in rb_call (scope=CALL_FCALL, argv=0x7fffffffbd60, argc=1, mid=<optimized out>,
recv=22956880) at vm_eval.c:616
#10 rb_funcall (recv=recv@entry=22956880, mid=<optimized out>, n=n@entry=1) at vm_eval.c:818
#11 0x00007ffff6092b10 in DL_RecvPdu () at CFGroundClient.c:414
#12 0x00007ffff6092dbe in method_run (self=<optimized out>) at CFGroundClient.c:302
#13 0x00007ffff7ace272 in vm_call_cfunc_with_frame (ci=0x0, reg_cfp=0x7ffff7fd1f70, th=0x6035d0)
at vm_insnhelper.c:1380
#14 vm_call_cfunc (th=th@entry=0x6035d0, reg_cfp=reg_cfp@entry=0x7ffff7fd1f70, ci=ci@entry=0xa29090)
at vm_insnhelper.c:1473
#15 0x00007ffff7ae0f6e in vm_call_method (th=0x6035d0, cfp=0x7ffff7fd1f70, ci=0xa29090) at vm_insnhelper.c:1689
#16 0x00007ffff7ad4b4b in vm_exec_core (th=th@entry=0x6035d0, initial=initial@entry=0) at insns.def:1069
#17 0x00007ffff7ad951b in vm_exec (th=th@entry=0x6035d0) at vm.c:1440
#18 0x00007ffff7adaa73 in rb_iseq_eval_main (iseqval=iseqval@entry=10626960) at vm.c:1685
#19 0x00007ffff798707d in ruby_exec_internal (n=0xa22790) at eval.c:254
#20 0x00007ffff798948d in ruby_exec_node (n=n@entry=0xa22790) at eval.c:319
#21 0x00007ffff798bc3e in ruby_run_node (n=0xa22790) at eval.c:311
#22 0x000000000040087b in main (argc=3, argv=0x7fffffffde58) at main.c:36

相关代码如下:

                void DL_RecvPdu() {
.....
/* Segfault coming from here
CUR_PACKET.read("PDU_DATA");
Reads telemetry item PDU_DATA from packet CUR_PACKET
and returns results. PDU data is an array of bytes
represented as a Ruby string that can contain nulls */
VALUE pdu_data_block = rb_funcall(rb_CUR_PACKET,
rb_READ, 1,
rb_PDU_DATA
);
....
}

有谁知道这个错误可能来自哪里以及如何修复它?

最佳答案

导致该问题的可能原因有很多,但(似乎)没有足够的数据。

一种解释可能是 Ruby 对象(即 rb_CUR_PACKETrb_READrb_PDU_DATA)在您调用时无效函数,因为它们可能已被 GC(垃圾收集器)收集。

如果 Ruby 对象没有引用这些对象,它们的内存可能会比您预期的更快被释放。

一个简单的解决方案是将对象注册为全局对象(它们永远不会被释放)或将它们分配为用户对象内的变量。

另一个可能的原因是您没有在 GIL(全局指令锁)内执行代码。

Ruby MRI 代码必须始终在 GIL 和 Ruby 线程内运行。如果您离开了 GIL 或生成了非 Ruby 线程的新线程,那么您的代码将会崩溃(如果您幸运的话)。

我怀疑GC,但这是一个折腾。

祝你好运!

关于c - Ruby C 扩展 : rb_funcall causing segfault,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45584572/

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