# Question about embedding ruby 1.9 and handling exceptions

**URL:** <https://www.ruby-forum.com/t/question-about-embedding-ruby-1-9-and-handling-exceptions/169379>\
**Category:** Ruby\
**Created:** [June 3, 2009, 2:44am UTC](https://www.ruby-forum.com/t/question-about-embedding-ruby-1-9-and-handling-exceptions/169379 "2009-06-03T02:44:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rolando\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d9b06d/32.png) [@Rolando\_A](https://www.ruby-forum.com/u/Rolando_A)\
**Post date:** [June 3, 2009, 2:44am UTC](https://www.ruby-forum.com/t/question-about-embedding-ruby-1-9-and-handling-exceptions/169379/1 "2009-06-03T02:44:00Z")

</div>

I’m embedding ruby 1.9, and so far everything works _almost_ ok: there  
is a problem with the exception handling when one is raised from code  
evaluated in ruby code (or ruby-c code) referenced from plain-c code.  
I’ll try to explain it with a simple flux:

app-init  
±-- 1) init ruby (load main.rb, which in turn load other ruby files)  
|  
±-- 2) do some c stuff  
|  
±-- 3) some c code will call a ruby method in a ruby object  
± 4) ruby code is executed, which might raise an exception

If an exception is raised on (1), it works as expected (I’m using  
rb\_protect(RUBY\_METHOD\_FUNC(rb\_require), (VALUE)“main”, &state) to  
“start” ruby). However, is an exception is raised during (4), I get an  
invalid access and the code crashes (when calling longjump), here’s  
the backtrace, which is always the same:

#0 0x91627288 in longjmp  
#1 0x0010cc62 in vm\_exec at vm.c:1261  
#2 0x00107037 in vm\_call0 at vm\_eval.c:57  
#3 0x00107ec6 in rb\_call0 at vm\_eval.c:249  
#4 0x00107b63 in rb\_call at vm\_eval.c:255  
#5 0x00107f8b in rb\_funcall at vm\_eval.c:427  
#6 0x000d1d16 in -[CocosNode(SC\_Extension) textFieldShouldReturn:] at  
SC\_CocosNode.m:108

#6 is the where I call the ruby code. If the exception is not raised,  
everything works just fine. The problem here is that this makes hard  
to track bugs in the ruby code…  
Any idea why this might be happening? Do I have to do something  
special when calling ruby code from the C world?  
Oh, and everything is in the same thread…

PS: using ruby 1.9.1 (checked out from svn branch 1\_9\_1):  
$ cat ruby/version.h  
#define RUBY\_VERSION “1.9.1”  
#define RUBY\_RELEASE\_DATE “2009-05-22”  
#define RUBY\_PATCHLEVEL 154  
#define RUBY\_VERSION\_MAJOR 1  
#define RUBY\_VERSION\_MINOR 9  
#define RUBY\_VERSION\_TEENY 1  
#define RUBY\_RELEASE\_YEAR 2009  
#define RUBY\_RELEASE\_MONTH 5  
#define RUBY\_RELEASE\_DAY 22

thanks!
