Search code examples
crubyruby-c-extension

How to call exit from a script loaded via rb_load_protect without a segfault


I want to quit the application with success from Ruby 2.3.3 (Win32). I get a segfault when I try to exit the program from that script.

I tried the following:

# script.rb
loop do
    # do something
    exit 0 if foo()
    break if bar()
end

foo and bar are just returning Qtrue or Qfalse.

I load the script with:

ruby_init();

int ruby_state = 0;
VALUE script = rb_str_new_cstr("./script.rb");
rb_load_protect(script, 0, &ruby_state);

ruby_cleanup(ruby_state);

and it produces a segfault. I also tried to put ruby_cleanup in a function called from Ruby, but it produces the same segfault.

How can I stop the Ruby VM, or at least stop the current script?


Solution

  • Try exit!. It ignores callbacks an so on.