Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this code not work on ruby 1.9 but works on ruby 1.8?

Tags:

c

ruby

I downloaded the lasted stable ruby source code from the ruby website (1.9.2-p180) and compiled it on Windows with MinGW 4.5.2-TDM and MSYS. To compile I ran sh configure and make. I got msvcrt-ruby191.dll and libmsvcrt-ruby191.dll.a exactly as expected. Then I wrote this code:

#include <ruby.h>
int main() {
  ruby_init();
  rb_funcall2(Qnil, rb_intern("p"), 1, (VALUE[]){INT2FIX(0)});
  ruby_finalize();
}

I compiled with g++, linking to the ruby's dll. When I ran the executable, I got this error message:

<main>: [BUG] Segmentation fault
ruby 1.9.2p180 (2011-02-18 revision 30909) [i386-mingw32]

-- control frame ----------
c:0002 p:---- s:0004 b:0004 l:000003 d:000003 CFUNC  :p
c:0001 p:0000 s:0002 b:0002 l:00120c d:00120c TOP
---------------------------
-- Ruby level backtrace information ----------------------------------------
ruby:0:in `p'

[NOTE]
You may have encountered a bug in the Ruby interpreter or extension libraries.
Bug reports are welcome.
For details: http://www.ruby-lang.org/bugreport.html


This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

The problem is: The same code works perfectly when linked with the ruby 1.8.7. What is wrong here?

like image 491
Guilherme Bernal Avatar asked Jul 05 '11 16:07

Guilherme Bernal


1 Answers

Please, try to extend your init to this:

int main(int argc,char *argv[]) {
  ruby_sysinit(&argc, &argv);
  {
    RUBY_INIT_STACK;
    ruby_init();
    ruby_init_loadpath();
    // ... code
    ruby_finalize();
  }
  return 0; //from main()
}

The initialization process was changed between 1.8 and 1.9 versions, so adding a separate nested block { .. } and a RUBY_INIT_STACK macro in the block are needed now.

like image 196
osgx Avatar answered Nov 15 '22 09:11

osgx