I’m embedding Ruby in a C++ program. This is working fine when I load
the Ruby runtimes (ruby_init() etc), then do some Ruby things, and then
terminate the Ruby runtimes (ruby_finalize() etc).
But if I try to do this more than once, I get a segfault from within a
Ruby API. I’m using 1.9.2-p180 (built from source) on RH14 Linux and I
also get the same issue on Windows XP.
To hopefully better explain the issue, my code does this kind of setup:
ruby_init();
ruby_init_loadpath();
and then does what it needs to do with Ruby, and then does this kind of
clean-up:
ruby_finalize();
ruby_cleanup(0);
That works fine, but if I try to do that sequence again, I get a
segfault on the call to ruby_init_loadpath(). Running valgrind shows
that the first error is in ruby_init_loadpath_safe, like so:
==5644== Invalid read of size 4
==5644== at 0x411C581: ruby_init_loadpath_safe (ruby.c:439)
==5644== by 0x411C69D: ruby_init_loadpath (ruby.c:335)
==5644== by 0x8048800: doTest (rr.c:34)
==5644== by 0x804884C: main (rr.c:50)
==5644== Address 0x50 is not stack’d, malloc’d or (recently) free’d
Am I doing something silly (such as using the Ruby API incorrectly) or
does Ruby not support this kind of use?
I’ve attached a minimal C test program which shows the issue.
Thanks.