# Problem reloading Ruby runtimes

**URL:** <https://www.ruby-forum.com/t/problem-reloading-ruby-runtimes/204849>\
**Category:** Ruby\
**Created:** [March 23, 2011, 8:19am UTC](https://www.ruby-forum.com/t/problem-reloading-ruby-runtimes/204849 "2011-03-23T08:19:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [March 23, 2011, 8:19am UTC](https://www.ruby-forum.com/t/problem-reloading-ruby-runtimes/204849/1 "2011-03-23T08:19:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![elborran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9dc877/32.png) [@elborran](https://www.ruby-forum.com/u/elborran)\
**Post date:** [March 23, 2011, 11:53pm UTC](https://www.ruby-forum.com/t/problem-reloading-ruby-runtimes/204849/2 "2011-03-23T23:53:02Z")

</div>

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 ruby-core@ 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.

---

<div class="post-metadata">

**Author:** ![elborran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9dc877/32.png) [@elborran](https://www.ruby-forum.com/u/elborran)\
**Post date:** [March 23, 2011, 11:58pm UTC](https://www.ruby-forum.com/t/problem-reloading-ruby-runtimes/204849/3 "2011-03-23T23:58:42Z")

</div>

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.
