Problem reloading Ruby runtimes

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.

On Mar 23, 2011, at 00:19 , Eugene Elborran wrote:

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).

You should send this to [email protected] instead… but afaik, ruby (at least
it was the case in 1.8) is just not very good at reinitializing the
runtime. That may have changed in 1.9, but I doubt it was much of a
priority. Python and Lua are excellent at this tho, so worst case you
can switch to one of them.

Thanks for the suggestion, yes I’ll try ruby-core.

We did consider using Lua for this project, it would be more flexible
for embedding but the language isn’t as good as Ruby.