Embedded socket error

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

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

    ruby_init();
    ruby_init_loadpath();
    ruby_script("embedded");
    rb_load_file( "embr.rb" );
    ruby_run();

Embed\rubyEmbed>Debug\rubyEmbed.exe
/socket.so: [BUG] Segmentation fault
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

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

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

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;
}

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