# Problems with Ruby and C++

**URL:** <https://www.ruby-forum.com/t/problems-with-ruby-and-c/63473>\
**Category:** Ruby\
**Created:** [June 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/problems-with-ruby-and-c/63473 "2006-06-15T14:20:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wooleywoolee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7feea3/32.png) [@wooleywoolee](https://www.ruby-forum.com/u/wooleywoolee)\
**Post date:** [June 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/problems-with-ruby-and-c/63473/1 "2006-06-15T14:20:00Z")

</div>

Hi all,

I’ve been using Ruby, SWIG and C++ on Linux for my development work and  
am encountering some problems. The C++ consists of a built dynamic  
library and I’m implementing SWIG the standard way  
[http://www.swig.org/Doc1.3/SWIGPlus.html](http://www.swig.org/Doc1.3/SWIGPlus.html)

The problem is, I seem to see memory getting corrupted when running the  
Ruby program which calls the C++ library. When the Ruby program is run  
on standalone, there are no problems. When the C++ dynamic library is  
run with an equivalent C++ program, there are no problems. This leads me  
to believe that there are some “interactions” between Ruby and the C++  
library. Also, the Ruby program runs on a different thread than the C++.

Has anyone encoutered anything like this before?

Out of sorts ☹  
Wooleywoolee

---

<div class="post-metadata">

**Author:** ![wooleywoolee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7feea3/32.png) [@wooleywoolee](https://www.ruby-forum.com/u/wooleywoolee)\
**Post date:** [June 15, 2006, 4:26pm UTC](https://www.ruby-forum.com/t/problems-with-ruby-and-c/63473/2 "2006-06-15T16:26:02Z")

</div>

> to believe that there are some “interactions” between Ruby and the C++  
> library. Also, the Ruby program runs on a different thread than the  
> C++.
> 
> Has anyone encoutered anything like this before?

I don’t really have much input, but maybe I can add some questions to  
spur discussion and prevent this post from dropping into obscurity 🙂

You’re using ruby in its own thread. Are you making calls into the  
ruby machine from the c++ thread? I’m not sure how thread-safe the  
ruby-library is, but I seem to remember that it’s not terribly safe.

I’ve had issues (with python/c++) where I failed to properly claim  
pointers to data recieved from python, so its garbage collector cleaned  
up the data that I was using. I don’t know how swig handles this (I  
don’t much like swig), but if your reference counts aren’t being  
incremented and decremented properly, that can cause trouble.

I’m hoping someone more knowledgeable than I can at least show why my  
questions are dumb or irrelevant. Maybe we can figure out what’s going  
on…

–jay

---

<div class="post-metadata">

**Author:** ![wooleywoolee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7feea3/32.png) [@wooleywoolee](https://www.ruby-forum.com/u/wooleywoolee)\
**Post date:** [June 19, 2006, 10:09pm UTC](https://www.ruby-forum.com/t/problems-with-ruby-and-c/63473/3 "2006-06-19T22:09:24Z")

</div>

On Thu, Jun 15, 2006 at 09:20:19PM +0900, Will Yeoh wrote:

> The problem is, I seem to see memory getting corrupted when running the  
> Ruby program which calls the C++ library. When the Ruby program is run  
> on standalone, there are no problems. When the C++ dynamic library is  
> run with an equivalent C++ program, there are no problems. This leads me  
> to believe that there are some “interactions” between Ruby and the C++  
> library. Also, the Ruby program runs on a different thread than the C++.

You can try running ruby under valgrind to find the problem. You will  
need to use a suppressions file, which you can find in [ruby-talk:52065]  
or at [http://rubystuff.org/ruby.supp](http://rubystuff.org/ruby.supp) .

Paul

---

<div class="post-metadata">

**Author:** ![wooleywoolee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7feea3/32.png) [@wooleywoolee](https://www.ruby-forum.com/u/wooleywoolee)\
**Post date:** [June 19, 2006, 10:26pm UTC](https://www.ruby-forum.com/t/problems-with-ruby-and-c/63473/4 "2006-06-19T22:26:12Z")

</div>

On 6/15/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > to believe that there are some “interactions” between Ruby and the C++  
> > library. Also, the Ruby program runs on a different thread than the  
> > C++.

> You’re using ruby in its own thread. Are you making calls into the  
> ruby machine from the c++ thread? I’m not sure how thread-safe the  
> ruby-library is, but I seem to remember that it’s not terribly safe.

It is definitely the case that calling into the ruby interpreter from  
anything other than the interpreter thread is a “bad thing.” If you  
need to signal from the C++ to the Ruby you have a number of choices,  
but calling any rb\_XXXX functions is not one of them. I am not  
familiar with SWIG’s particulars, but I am guessing this means that  
accessing _any_ of the ruby methods from a thread other than the  
interpreter will lead to undefined behavior (here there be monsters).

pth

---

<div class="post-metadata">

**Author:** ![wooleywoolee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7feea3/32.png) [@wooleywoolee](https://www.ruby-forum.com/u/wooleywoolee)\
**Post date:** [June 20, 2006, 3:13am UTC](https://www.ruby-forum.com/t/problems-with-ruby-and-c/63473/5 "2006-06-20T03:13:53Z")

</div>

What platform are you running this on? When you say the C++ code runs on  
a  
different thread, do you mean a different Ruby thread or a different  
native  
thread? If the latter, how do you know? Does your C++ library spin a  
thread?  
Do you source for the C++ library? Finally, did you build Ruby with  
pthread  
support?
