crash of extension on rb_str_new2

N

newbie

Hi, all
I am integrating a 3rd party API with ruby extension. The code is
roughly like this
void my_function()
{
char p[256];
...//assign the value of p in the api

return rb_str_new2(p);//This cause a crash.
}
while if I change the last part a little bit as,
VALUE str=rb_str_new2(p);
return str;
This seems not crashing

It looks like more related to ruby GC? Can anyone explain on this? and
how to prevent this kind of crash?

Thanks!
 
D

daz

newbie said:
Hi, all
I am integrating a 3rd party API with ruby extension. The code is
roughly like this
void my_function()
{
char p[256];
...//assign the value of p in the api

return rb_str_new2(p);//This cause a crash.
}
while if I change the last part a little bit as,
VALUE str=rb_str_new2(p);
return str;
This seems not crashing

It looks like more related to ruby GC? Can anyone explain on this? and
how to prevent this kind of crash?

Thanks!

I guess changing:
void my_function()

to:
VALUE my_function()

might help (?)


daz
 
E

Eero Saynatkari

newbie said:
Hi, all
I am integrating a 3rd party API with ruby extension. The code is
roughly like this
void my_function()
{
char p[256];
...//assign the value of p in the api

return rb_str_new2(p);//This cause a crash.
}
while if I change the last part a little bit as,
VALUE str=rb_str_new2(p);
return str;
This seems not crashing

It looks like more related to ruby GC? Can anyone explain on this? and
how to prevent this kind of crash?

p is a pointer to a local array which may be causing
the problem since the memory is released when you exit
the function's scope. You should probably be malloc()ing
the original string to char* p instead just in general
C programming terms.

That said, I am not quite sure why the second version
would work if that were the underlying cause here.


E
 
E

Eero Saynatkari

Eero said:
newbie said:
Hi, all
I am integrating a 3rd party API with ruby extension. The code is
roughly like this
void my_function()
{
char p[256];
...//assign the value of p in the api

return rb_str_new2(p);//This cause a crash.
}
while if I change the last part a little bit as,
VALUE str=rb_str_new2(p);
return str;
This seems not crashing

It looks like more related to ruby GC? Can anyone explain on this? and
how to prevent this kind of crash?


p is a pointer to a local array which may be causing
the problem since the memory is released when you exit
the function's scope. You should probably be malloc()ing
the original string to char* p instead just in general
C programming terms.

That said, I am not quite sure why the second version
would work if that were the underlying cause here.

Bah, too little C lately. The function's return type should be
VALUE, not void (if you have this in your actual code).

Warning about the local array still stands :)

E
 
N

newbie

Thanks for the quick reply!

Sorry for the confusing of "void", I just show my code conceptaully. In
fact it is some thing like this,
VALUE create_instance(int argc, VALUE *argv, VALUE self)
and I create it in the class as
rb_define_method(cMyXXXX, "create_instance",
(ruby_method*)&create_instance, -1);

I think your local array stands also. I have tested with the option of
static char p[256];
But, too bad, it's still the same.

Rdgs.
 
N

nobuyoshi nakada

Hi,

At Tue, 8 Nov 2005 17:57:12 +0900,
newbie wrote in [ruby-talk:164738]:
Hi, all
I am integrating a 3rd party API with ruby extension. The code is
roughly like this
void my_function()
{
char p[256];
...//assign the value of p in the api

Is it certainly terminated by '\0'?
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top