[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

multiple rb_require()s causing segmentation fault

Zaratustra

11/30/2004 2:15:00 AM

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?

3 Answers

leon breedt

11/30/2004 3:01:00 AM

0

On Tue, 30 Nov 2004 11:17:49 +0900, zarawesome@gmail.com
<zarawesome@gmail.com> wrote:

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


Zaratustra

11/30/2004 3:19:00 AM

0


leon breedt wrote:
> On Tue, 30 Nov 2004 11:17:49 +0900, zarawesome@gmail.com
> <zarawesome@gmail.com> wrote:
>
> 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:
>
> > 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.

Altering it does not affect the problem in any way.

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

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

Zaratustra

11/30/2004 8:55:00 PM

0

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