# Embedded socket error

**URL:** <https://www.ruby-forum.com/t/embedded-socket-error/100051>\
**Category:** Ruby\
**Created:** [July 26, 2006, 9:35pm UTC](https://www.ruby-forum.com/t/embedded-socket-error/100051 "2006-07-26T21:35:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![laserwhit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@laserwhit](https://www.ruby-forum.com/u/laserwhit)\
**Post date:** [July 26, 2006, 9:35pm UTC](https://www.ruby-forum.com/t/embedded-socket-error/100051/1 "2006-07-26T21:35:41Z")

</div>

I am running the following code in a C++ app:

int \_tmain(int argc, \_TCHAR\* argv[])  
{  
NtInitialize(&argc, &argv);  
ruby\_init();  
ruby\_init\_loadpath();  
ruby\_script(“embedded”);  
rb\_load\_file( “embr.rb” );  
ruby\_run();  
return 0;  
}

with embr.rb of:  
require ‘socket’

I get:

* * *

Embed\rubyEmbed\>Debug\rubyEmbed.exe  
./socket.so: [BUG] Segmentation fault  
ruby 1.8.2 (2004-12-25) [i386-mswin32]

This application has requested the Runtime to terminate it in an unusual  
way.  
Please contact the application’s support team for more information.

* * *

Any ideas?

Nathan

---

<div class="post-metadata">

**Author:** ![laserwhit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@laserwhit](https://www.ruby-forum.com/u/laserwhit)\
**Post date:** [July 27, 2006, 10:36am UTC](https://www.ruby-forum.com/t/embedded-socket-error/100051/2 "2006-07-27T10:36:52Z")

</div>

> > > > > “N” == Nathan W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

N\> ruby\_init();  
N\> ruby\_init\_loadpath();  
N\> ruby\_script(“embedded”);  
N\> rb\_load\_file( “embr.rb” );  
N\> ruby\_run();

N\> Embed\rubyEmbed\>Debug\rubyEmbed.exe  
N\> /socket.so: [BUG] Segmentation fault  
N\> ruby 1.8.2 (2004-12-25) [i386-mswin32]

it’s normal.

When you write an embedded application, you have the responsibility to  
protect your application against the error that ruby can raise. In your  
case, rb\_load\_file() can raise an exception (file not found, …) and  
it  
must be called inside a call to rb\_protect() to give you the  
possibility  
to test the error.

It’s also best if you use a more recent ruby.

–

Guy Decoux

---

<div class="post-metadata">

**Author:** ![laserwhit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@laserwhit](https://www.ruby-forum.com/u/laserwhit)\
**Post date:** [July 27, 2006, 6:13pm UTC](https://www.ruby-forum.com/t/embedded-socket-error/100051/3 "2006-07-27T18:13:57Z")

</div>

So I installed 1.8.4 and couldn’t compile until I changed the  
C:\ruby\lib\ruby\1.8\i386-mswin32\config.h to accept \_MSC\_VER \> 1200. I  
then changed my code to:

rubyEmbeded.cpp:

* * *

#include “stdafx.h”  
#include “ruby.h”  
#include “win32\win32.h”

static VALUE wrap\_run( VALUE arg1 ) {  
rb\_load\_file( StringValueCStr( arg1 ) );  
return Qnil;  
}

static VALUE protected\_run( VALUE script ) {  
int error;  
VALUE args[1];  
VALUE result;

```
args[0] = script;

result = rb_protect( wrap_run, (VALUE)args, &error );
return error ? Qnil : result;

```

}

int \_tmain(int argc, \_TCHAR\* argv[])  
{  
NtInitialize(&argc, &argv);  
ruby\_init();  
ruby\_init\_loadpath();  
ruby\_script(“embedded”);

```
VALUE script = rb_str_new( "embr.rb", strlen("embr.rb") );
protected_run( script );

ruby_run();

return 0;

```

}

* * *

embr.rb:

* * *

require ‘socket’

* * *

My results are still:

Embed\rubyEmbed\>Debug\rubyEmbed.exe  
embedded: [BUG] Segmentation fault  
ruby 1.8.4 (2006-04-14) [i386-mswin32]

This application has requested the Runtime to terminate it in an unusual  
way.  
Please contact the application’s support team for more information.

Any more thoughts? I’m not sure how I’m supposed to debug this. By the  
way thanks for you help so far.

Nathan

---

<div class="post-metadata">

**Author:** ![laserwhit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@laserwhit](https://www.ruby-forum.com/u/laserwhit)\
**Post date:** [July 27, 2006, 6:49pm UTC](https://www.ruby-forum.com/t/embedded-socket-error/100051/4 "2006-07-27T18:49:01Z")

</div>

> > > > > “N” == Nathan W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

N\> static VALUE wrap\_run( VALUE arg1 ) {  
^^^^^^^^^^  
N\> rb\_load\_file( StringValueCStr( arg1 ) );  
^^^^  
N\> return Qnil;  
N\> }

N\> static VALUE protected\_run( VALUE script ) {  
N\> int error;  
N\> VALUE args[1];  
^^^^^^^^^^^^^  
N\> VALUE result;

N\> args[0] = script;  
^^^^^^^

N\> result = rb\_protect( wrap\_run, (VALUE)args, &error );  
^^^^  
N\> return error ? Qnil : result;  
N\> }

You give a C array to wrap\_run() and then inside wrap\_run() you  
interpret  
it as a VALUE.

Now if you want to do something similar to “load ‘embed.rb’”, you have  
the function

void rb\_load\_protect(VALUE fname, int wrap, int \*state);

which call rb\_load() inside the equivalent of rb\_protect()

Guy Decoux
