crash of extension on rb_str_new2

Discussion in 'Ruby' started by newbie, Nov 8, 2005.

  1. newbie

    newbie Guest

    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!
     
    newbie, Nov 8, 2005
    #1
    1. Advertising

  2. newbie

    daz Guest

    newbie wrote:
    > 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
     
    daz, Nov 8, 2005
    #2
    1. Advertising

  3. newbie wrote:
    > 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.

    > Thanks!



    E
     
    Eero Saynatkari, Nov 8, 2005
    #3
  4. Eero Saynatkari wrote:
    > newbie wrote:
    >
    >> 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 :)

    >> Thanks!


    E
     
    Eero Saynatkari, Nov 8, 2005
    #4
  5. newbie

    newbie Guest

    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.
     
    newbie, Nov 8, 2005
    #5
  6. newbie

    daz Guest

    Eero Saynatkari wrote:
    >
    > Warning about the local array still stands :)
    >


    The new string is a malloc'd copy of the local array.

    daz
     
    daz, Nov 8, 2005
    #6
  7. 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'?

    --
    Nobu Nakada
     
    nobuyoshi nakada, Nov 9, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Giles Brown

    Help with Extension Class Crash

    Giles Brown, Jan 28, 2004, in forum: Python
    Replies:
    1
    Views:
    269
    Giles Brown
    Jan 28, 2004
  2. MKPrasad
    Replies:
    1
    Views:
    268
    Bob Barrows
    Dec 5, 2003
  3. Hadmut Danisch

    rb_str_new2

    Hadmut Danisch, Apr 10, 2004, in forum: Ruby
    Replies:
    2
    Views:
    697
  4. Daniel Berger

    rb_str_new2, malloc, free - core dump

    Daniel Berger, Mar 3, 2005, in forum: Ruby
    Replies:
    1
    Views:
    322
    Charles Mills
    Mar 3, 2005
  5. barabba
    Replies:
    5
    Views:
    277
    barabba
    Apr 12, 2007
Loading...

Share This Page