使用Singleton模块在C中的红宝石崩溃

发布于 2025-01-22 14:07:42 字数 1852 浏览 3 评论 0原文

我试图使用Ruby's C API实施一个项目,这使我遇到了以下问题。 为使用以下代码:

#include <ruby.h>

int main(int argc, char* argv[])
{
  ruby_init();
  rb_require("singleton");
  return ruby_cleanup(0);
}

我有一个脚本,需要单顿模块,并注意到我的程序总是崩溃,所以我将问题归结

gcc test.c `pkg-config --cflags --libs ruby`

每当我运行此操作时,我都会在rb_require(“ Singletleton”)上进行segfault(“ Singleton” )

ruby: [BUG] Segmentation fault at 0x00000c
ruby 2.3.0p0 (2015-12-25) [i386-linux-gnu]

-- Control frame information -----------------------------------------------
c:0001 p:0000 s:0002 E:001788 (none) [FINISH]


-- Machine register context ------------------------------------------------
  GS: 0x00000063  FS: 0x00000000  ES: 0x0000002b  DS: 0x0000002b EDI: 0x098163e8
 ESI: 0xf7f7f230 EBP: 0xff8004c8 ESP: 0xff8004c4 EBX: 0x00000006 EDX: 0x00000000
 ECX: 0x00000006 EAX: 0x09816410 TRA: 0x0000000e ERR: 0x00000004 EIP: 0xf7d29d76
  CS: 0x00000023 EFL: 0x00010212 UES: 0xff8004c4  SS: 0x0000002b

-- C level backtrace information -------------------------------------------
/lib/i386-linux-gnu/libruby-2.3.so.2.3 [0xf7e49c41]
/lib/i386-linux-gnu/libruby-2.3.so.2.3 [0xf7e49e33]
/lib/i386-linux-gnu/libruby-2.3.so.2.3 [0xf7d267cc]
/lib/i386-linux-gnu/libruby-2.3.so.2.3 [0xf7dd4493]
linux-gate.so.1 [0xf7f9e090]
/lib/i386-linux-gnu/libruby-2.3.so.2.3 [0xf7d29d76]
/lib/i386-linux-gnu/libruby-2.3.so.2.3 [0xf7d2ae68]
/lib/i386-linux-gnu/libruby-2.3.so.2.3 [0xf7d2b2d1]
/lib/i386-linux-gnu/libruby-2.3.so.2.3 [0xf7d26e2b]
/lib/i386-linux-gnu/libruby-2.3.so.2.3 [0xf7d28734]
/lib/i386-linux-gnu/libruby-2.3.so.2.3 [0xf7d30903]
/lib/i386-linux-gnu/libruby-2.3.so.2.3(rb_require+0x3a) [0xf7d309da]
./a.out(main+0x23) [0x80485ee]

我在不同的机器上使用了几个Ruby版本(2.3、2.5和2.7)尝试了一下,并始终遇到同一问题,因此目前我认为我在做错了什么。

有人可以解释一下这里的问题吗?

I was trying to implement a project using Ruby's C API which led me to the following problem. I have a script that requires the Singleton module and noticed that my program always crashes, so I boiled the issue down to using the following code:

#include <ruby.h>

int main(int argc, char* argv[])
{
  ruby_init();
  rb_require("singleton");
  return ruby_cleanup(0);
}

Compile it using

gcc test.c `pkg-config --cflags --libs ruby`

Whenever I run this I get a segfault at the rb_require("singleton").

ruby: [BUG] Segmentation fault at 0x00000c
ruby 2.3.0p0 (2015-12-25) [i386-linux-gnu]

-- Control frame information -----------------------------------------------
c:0001 p:0000 s:0002 E:001788 (none) [FINISH]


-- Machine register context ------------------------------------------------
  GS: 0x00000063  FS: 0x00000000  ES: 0x0000002b  DS: 0x0000002b EDI: 0x098163e8
 ESI: 0xf7f7f230 EBP: 0xff8004c8 ESP: 0xff8004c4 EBX: 0x00000006 EDX: 0x00000000
 ECX: 0x00000006 EAX: 0x09816410 TRA: 0x0000000e ERR: 0x00000004 EIP: 0xf7d29d76
  CS: 0x00000023 EFL: 0x00010212 UES: 0xff8004c4  SS: 0x0000002b

-- C level backtrace information -------------------------------------------
/lib/i386-linux-gnu/libruby-2.3.so.2.3 [0xf7e49c41]
/lib/i386-linux-gnu/libruby-2.3.so.2.3 [0xf7e49e33]
/lib/i386-linux-gnu/libruby-2.3.so.2.3 [0xf7d267cc]
/lib/i386-linux-gnu/libruby-2.3.so.2.3 [0xf7dd4493]
linux-gate.so.1 [0xf7f9e090]
/lib/i386-linux-gnu/libruby-2.3.so.2.3 [0xf7d29d76]
/lib/i386-linux-gnu/libruby-2.3.so.2.3 [0xf7d2ae68]
/lib/i386-linux-gnu/libruby-2.3.so.2.3 [0xf7d2b2d1]
/lib/i386-linux-gnu/libruby-2.3.so.2.3 [0xf7d26e2b]
/lib/i386-linux-gnu/libruby-2.3.so.2.3 [0xf7d28734]
/lib/i386-linux-gnu/libruby-2.3.so.2.3 [0xf7d30903]
/lib/i386-linux-gnu/libruby-2.3.so.2.3(rb_require+0x3a) [0xf7d309da]
./a.out(main+0x23) [0x80485ee]

I tried this with several Ruby versions (2.3, 2.5 and 2.7) on different machines and always run into the same issue, so at the moment I think I am doing something wrong.

Can someone explain what might be the issue here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生来就爱笑 2025-01-29 14:07:42

为了使用RB_REQUIRE首先需要调用Ruby_init_loadpath首先。

因此,这起作用:

int main(int argc, char* argv[])
{
  ruby_init();
  ruby_init_loadpath();

  rb_require("singleton");

  return ruby_cleanup(0);
}

在研究您的问题时,我发现它们很有用:
在C
中运行Ruby
您如何完全初始化嵌入式红宝石C ++应用程序中的VM?

In order to use rb_require you need to call ruby_init_loadpath first.

So, this works:

int main(int argc, char* argv[])
{
  ruby_init();
  ruby_init_loadpath();

  rb_require("singleton");

  return ruby_cleanup(0);
}

While researching your problem I found these to be useful:
Running Ruby in C
How do you fully initialize an embedded ruby VM in a C++ application?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文