1

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?

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

1 Comment

I actually used these manuals but missed that information. It works, thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.