Embedded socket error

N

Nathan Whitney

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
 
T

ts

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

Nathan Whitney

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
 
T

ts

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,871
Messages
2,569,919
Members
46,171
Latest member
A.N.Omalum

Latest Threads

Top