Consequences of not calling ruby_finalize()?

Discussion in 'Ruby' started by Phil Tomson, May 2, 2005.

  1. Phil Tomson

    Phil Tomson Guest

    Instead of embedding Ruby into a C program with a main() function I want
    to embed Ruby in a C library. The library is then linked with some other
    code which has a main() function. Here's my C code:

    //begin C code
    #include <ruby.h>

    static VALUE
    protected_require()
    { //TODO: for testing only, add protection later
    return rb_require("foo.rb");
    }
    static int initialized = 0;

    static VALUE summer ;

    static initialize_ruby()
    {
    if (!initialized) //only initialize once
    {
    int value;
    ruby_init();
    ruby_init_loadpath();
    ruby_script("embedded");
    rb_protect(protected_require, Qnil, &value);
    if (value) {
    VALUE err = rb_inspect(rb_gv_get("$!"));
    fprintf(stderr, "ERR %s\n", StringValuePtr(err));
    }
    summer = rb_class_new_instance(0,0,
    rb_const_get(rb_cObject,rb_intern("Summer")));
    initialized = 1;
    }
    }

    int sum(int max)
    {
    initialize_ruby();
    int id_sum = rb_intern("sum");
    VALUE result = rb_funcall(summer, id_sum,1,INT2NUM(max));
    //ruby_finalize(); //seems to work OK without it
    return NUM2INT(result);
    }

    int foo(int max)
    {
    initialize_ruby();
    int id_foo = rb_intern("foo");
    VALUE result = rb_funcall(summer, id_foo,1,INT2NUM(max));
    //ruby_finalize(); //seems to work OK without it
    return NUM2INT(result);

    }

    //end C code

    So I keep track of whether or not ruby has been initialized. Each of the
    functions in the library calls the initialize_ruby() function first
    thing. initialize_ruby() only really initializes ruby the first time
    (in order to save some time - I don't want to have to startup ruby for
    every function call and then finalize ruby at the end of each function
    call). Since I don't know what the last call to the function library
    might be, I don't call ruby_finalize() in any of the library functions.
    I used to do this, but eliminating the ruby_finalize() call seems to have
    no ill-effects. What are the consequences of not calling ruby_finalize()?

    Phil
     
    Phil Tomson, May 2, 2005
    #1
    1. Advertising

  2. Phil Tomson

    ts Guest

    >>>>> "P" == Phil Tomson <> writes:

    P> no ill-effects. What are the consequences of not calling ruby_finalize()?

    Like the name say it, it run proc and finalizer at the end of ruby.

    plruby (ruby embedded in postgres) never call it, volontary.


    Guy Decoux
     
    ts, May 2, 2005
    #2
    1. Advertising

  3. Phil Tomson

    Guest

    On Mon, 2 May 2005, ts wrote:

    >>>>>> "P" == Phil Tomson <> writes:

    >
    > P> no ill-effects. What are the consequences of not calling ruby_finalize()?
    >
    > Like the name say it, it run proc and finalizer at the end of ruby.
    >
    > plruby (ruby embedded in postgres) never call it, volontary.


    why?

    -a
    --
    ===============================================================================
    | email :: ara [dot] t [dot] howard [at] noaa [dot] gov
    | phone :: 303.497.6469
    | renunciation is not getting rid of the things of this world, but accepting
    | that they pass away. --aitken roshi
    ===============================================================================
     
    , May 2, 2005
    #3
  4. Phil Tomson

    ts Guest

    >>>>> "A" == Ara T Howard <> writes:

    A> On Mon, 2 May 2005, ts wrote:
    >>
    >> plruby (ruby embedded in postgres) never call it, volontary.


    A> why?

    To don't give the possibility to the user to run code with $SAFE < 12.
    There was a problem with old version of ruby.



    Guy Decoux
     
    ts, May 2, 2005
    #4
  5. Phil Tomson

    Guest

    On Mon, 2 May 2005, ts wrote:

    >>>>>> "A" == Ara T Howard <> writes:

    >
    > A> On Mon, 2 May 2005, ts wrote:
    >>>
    >>> plruby (ruby embedded in postgres) never call it, volontary.

    >
    > A> why?
    >
    > To don't give the possibility to the user to run code with $SAFE < 12.
    > There was a problem with old version of ruby.


    i assume the problem was that $SAFE could be elevated or ignored in
    finalizers?

    -a
    --
    ===============================================================================
    | email :: ara [dot] t [dot] howard [at] noaa [dot] gov
    | phone :: 303.497.6469
    | renunciation is not getting rid of the things of this world, but accepting
    | that they pass away. --aitken roshi
    ===============================================================================
     
    , May 2, 2005
    #5
  6. Phil Tomson

    ts Guest

    >>>>> "A" == Ara T Howard <> writes:

    A> i assume the problem was that $SAFE could be elevated or ignored in
    A> finalizers?

    Yes, I've given an example in ruby-talk where a finalizer was defined with
    $SAFE = 4 but run with $SAFE = 0



    Guy Decoux
     
    ts, May 2, 2005
    #6
  7. Phil Tomson

    Saynatkari Guest

    Le 2/5/2005, "(Phil Tomson)" <> a écrit:
    >Instead of embedding Ruby into a C program with a main() function I want
    >to embed Ruby in a C library. The library is then linked with some other
    >code which has a main() function. Here's my C code:
    >
    >//begin C code
    >#include <ruby.h>
    >
    >static VALUE
    >protected_require()
    >{ //TODO: for testing only, add protection later
    > return rb_require("foo.rb");
    >}
    >static int initialized = 0;
    >
    >static VALUE summer ;
    >
    >static initialize_ruby()
    >{
    > if (!initialized) //only initialize once
    > {
    > int value;
    > ruby_init();
    > ruby_init_loadpath();
    > ruby_script("embedded");
    > rb_protect(protected_require, Qnil, &value);
    > if (value) {
    > VALUE err = rb_inspect(rb_gv_get("$!"));
    > fprintf(stderr, "ERR %s\n", StringValuePtr(err));
    > }
    > summer = rb_class_new_instance(0,0,
    >rb_const_get(rb_cObject,rb_intern("Summer")));
    > initialized = 1;
    > }
    >}


    You may want to employ some sort of a file lock for concurrency
    if this is to be a shared library. I am not sure how the interpreter
    would behave there.

    >int sum(int max)
    >{
    > initialize_ruby();
    > int id_sum = rb_intern("sum");
    > VALUE result = rb_funcall(summer, id_sum,1,INT2NUM(max));
    > //ruby_finalize(); //seems to work OK without it
    > return NUM2INT(result);
    >}
    >
    >int foo(int max)
    >{
    > initialize_ruby();
    > int id_foo = rb_intern("foo");
    > VALUE result = rb_funcall(summer, id_foo,1,INT2NUM(max));
    > //ruby_finalize(); //seems to work OK without it
    > return NUM2INT(result);
    >
    >}
    >
    >//end C code
    >
    >So I keep track of whether or not ruby has been initialized. Each of the
    >functions in the library calls the initialize_ruby() function first
    >thing. initialize_ruby() only really initializes ruby the first time
    >(in order to save some time - I don't want to have to startup ruby for
    >every function call and then finalize ruby at the end of each function
    >call). Since I don't know what the last call to the function library
    >might be, I don't call ruby_finalize() in any of the library functions.
    >I used to do this, but eliminating the ruby_finalize() call seems to have
    >no ill-effects. What are the consequences of not calling ruby_finalize()?
    >
    >Phil


    E

    --
    template<typename duck>
    void quack(duck& d) { d.quack(); }
     
    Saynatkari, May 2, 2005
    #7
  8. Phil Tomson

    tsuraan Guest

    On May 2, 2005, at 10:19 AM, ts wrote:

    >>>>>> "A" == Ara T Howard <> writes:

    >
    > A> i assume the problem was that $SAFE could be elevated or ignored in
    > A> finalizers?
    >
    > Yes, I've given an example in ruby-talk where a finalizer was defined
    > with
    > $SAFE = 4 but run with $SAFE = 0


    Is this still a problem? Are $SAFE levels to be trusted, or is it
    worthless?
     
    tsuraan, May 3, 2005
    #8
  9. On 5/2/05, Phil Tomson <> wrote:
    > So I keep track of whether or not ruby has been initialized. Each of the
    > functions in the library calls the initialize_ruby() function first
    > thing. initialize_ruby() only really initializes ruby the first time
    > (in order to save some time - I don't want to have to startup ruby for
    > every function call and then finalize ruby at the end of each function
    > call). Since I don't know what the last call to the function library
    > might be, I don't call ruby_finalize() in any of the library functions.
    > I used to do this, but eliminating the ruby_finalize() call seems to have
    > no ill-effects. What are the consequences of not calling ruby_finalize()?
    >
    > Phil
    >
    >


    Could you conceivably use atexit() to to finalize ruby?
     
    Logan Capaldo, May 3, 2005
    #9
    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. Peter Rilling
    Replies:
    0
    Views:
    444
    Peter Rilling
    Aug 27, 2004
  2. Replies:
    6
    Views:
    600
  3. Steven Bethard
    Replies:
    6
    Views:
    626
    Jorge Luiz Godoy Filho
    Dec 29, 2004
  4. Qu0ll
    Replies:
    22
    Views:
    2,930
  5. Robert Dailey
    Replies:
    2
    Views:
    301
    Aaron Brady
    Oct 26, 2008
Loading...

Share This Page