Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Consequences of not calling ruby_finalize()?

Reply
Thread Tools

Consequences of not calling ruby_finalize()?

 
 
Phil Tomson
Guest
Posts: n/a
 
      05-02-2005
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
 
Reply With Quote
 
 
 
 
ts
Guest
Posts: n/a
 
      05-02-2005
>>>>> "P" == Phil Tomson <(E-Mail Removed)> 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





 
Reply With Quote
 
 
 
 
Ara.T.Howard@noaa.gov
Guest
Posts: n/a
 
      05-02-2005
On Mon, 2 May 2005, ts wrote:

>>>>>> "P" == Phil Tomson <(E-Mail Removed)> 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
================================================== =============================

 
Reply With Quote
 
ts
Guest
Posts: n/a
 
      05-02-2005
>>>>> "A" == Ara T Howard <(E-Mail Removed)> 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


 
Reply With Quote
 
Ara.T.Howard@noaa.gov
Guest
Posts: n/a
 
      05-02-2005
On Mon, 2 May 2005, ts wrote:

>>>>>> "A" == Ara T Howard <(E-Mail Removed)> 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
================================================== =============================

 
Reply With Quote
 
ts
Guest
Posts: n/a
 
      05-02-2005
>>>>> "A" == Ara T Howard <(E-Mail Removed)> 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





 
Reply With Quote
 
Saynatkari
Guest
Posts: n/a
 
      05-02-2005

Le 2/5/2005, "(Phil Tomson)" <(E-Mail Removed)> 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(); }



 
Reply With Quote
 
tsuraan
Guest
Posts: n/a
 
      05-03-2005

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

>>>>>> "A" == Ara T Howard <(E-Mail Removed)> 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?



 
Reply With Quote
 
Logan Capaldo
Guest
Posts: n/a
 
      05-03-2005
On 5/2/05, Phil Tomson <(E-Mail Removed)> 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?



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Okay, not much chat going on, so suffer the consequences... =?ISO-8859-1?Q?R=F4g=EAr?= Computer Support 1 03-09-2006 06:01 PM
generics: is this a good/bad idea; and eclipse consequences allen@rrsg.ee.uct.ac.za Java 6 09-21-2005 08:34 AM
consequences of not calling object.__init__? Steven Bethard Python 6 12-29-2004 03:20 AM
Consequences of streaking? Brian Computer Support 20 12-16-2004 02:04 PM
Consequences of using "nt authority\network service"? Peter Rilling ASP .Net 0 08-27-2004 05:04 PM



Advertisments