Problems with Ruby and C++

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

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 :frowning:
Wooleywoolee

···

--
Posted via http://www.ruby-forum.com/.

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
SWIG and C++

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?

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 :slight_smile:

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

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 .

Paul

···

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

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?

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

···

On 6/15/06, tsuraan@tsuraan.net <tsuraan@tsuraan.net> 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.