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