multiple rb_require()s causing segmentation fault

Z

zarawesome

Greetings,
I have been running into trouble with Ruby 1.8.1. I have created the
following code to load several files of code to the program:

// -------------------- code follows

char *filename_safe_require;

VALUE SafeRequire(VALUE arg)
{
return rb_require(filename_safe_require);
}

void ScriptLoadFile(char *filename)
{
filename_safe_require=filename;
VALUE info;
int error = 0;
rb_protect((unsigned long (__cdecl *)(unsigned long))SafeRequire, 0,
&error);
if(error)
{
rb_backtrace();
info = rb_inspect(ruby_errinfo);
Log("ERROR %s\n", STR2CSTR(info));
return;
}

[...other code...]
}

int main(int argc, char *argv[])
{
ruby_init();
ruby_init_loadpath();
ruby_script("embedded");

[Prepares classes for use in Ruby code]

ScriptLoadFile("scripts/player.rb", "");
ScriptLoadFile("scripts/enemy.rb", "");
ScriptLoadFile("scripts/boss.rb", "");
ScriptLoadFile("scripts/maps.rb", "");
ScriptLoadFile("scripts/objects.rb", "");
[...other code...]
}

// -------------------- code ends

The problem is, now, adding some lines to, say, enemy.rb will make the
program core dump while reading the -next- file. For example, adding a
line as prosaic as '$global=0' to a class function definition in
enemy.rb will crash it. However, removing every other class from the
enemy.rb file and adding that line works just fine.

I am under the impression that I'm hitting some kind of memory limit.
Is Ruby to blame or my program? Is there a way to check on this?
 
L

leon breedt

On Tue, 30 Nov 2004 11:17:49 +0900, (e-mail address removed)

On the face of it, I can't immediately see the cause of the segfault,
though I suspect you may have memory corruption somewhere else, which
is just being exposed by ScriptLoadFile.

There are some problems with the code you gave, though:
char *filename_safe_require;
^^^ not a good idea, thread-unsafe, so you'll run into problems when
Ruby starts supporting native OS threads, and you're exporting the
symbol "filename_safe_require", which may not strictly be necessary.
it will be safer to use stack-allocated storage for the parameter
passing from ScriptLoadFile.
void ScriptLoadFile(char *filename)
this does not match up with the way you are calling ScriptLoadFile
(you are passing a second parameter in your invocations).

to use stack allocation, i'd do something like:

char *args[1];
int error;

args[0] = filename;
error = 0;
rb_protect(SafeRequire, (VALUE)args, &error);


and modify SafeRequire to look something like:

static VALUE SafeRequire(VALUE arg)
{
char **args;
args = (char**)arg;
rb_require(args[0]);
}


disclaimer: i haven't tested this code, i reserve the right to hide if
brown paper bag errors are found :)

leon
 
Z

zarawesome

leon said:
On Tue, 30 Nov 2004 11:17:49 +0900, (e-mail address removed)

On the face of it, I can't immediately see the cause of the segfault,
though I suspect you may have memory corruption somewhere else, which
is just being exposed by ScriptLoadFile.

I do not see where such a memory corruption would happen, though, given
all the Ruby initialization is at the very start of the code.
There are some problems with the code you gave, though:

^^^ not a good idea, thread-unsafe, so you'll run into problems when
Ruby starts supporting native OS threads, and you're exporting the
symbol "filename_safe_require", which may not strictly be necessary.
it will be safer to use stack-allocated storage for the parameter
passing from ScriptLoadFile.

Altering it does not affect the problem in any way.
this does not match up with the way you are calling ScriptLoadFile
(you are passing a second parameter in your invocations).

You have probably noticed the code has been edited down. The second
parameter has no function at the moment.
 
Z

zarawesome

Updating to the latest stable snapshot has solved the problem. Thanks
for the time and attention.
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top