[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Consequences of not calling ruby_finalize()?

ptkwt

5/2/2005 4:47:00 AM

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
24 Answers

ts

5/2/2005 11:44:00 AM

0

>>>>> "P" == Phil Tomson <ptkwt@aracnet.com> 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





Ara.T.Howard

5/2/2005 1:38:00 PM

0

ts

5/2/2005 2:19:00 PM

0

>>>>> "A" == Ara T Howard <Ara.T.Howard@noaa.gov> 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


Ara.T.Howard

5/2/2005 2:59:00 PM

0

ts

5/2/2005 3:20:00 PM

0

>>>>> "A" == Ara T Howard <Ara.T.Howard@noaa.gov> 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





ES

5/2/2005 5:41:00 PM

0


Le 2/5/2005, "(Phil Tomson)" <ptkwt@aracnet.com> 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(); }



tsuraan

5/3/2005 1:12:00 AM

0


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

>>>>>> "A" == Ara T Howard <Ara.T.Howard@noaa.gov> 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?



Logan Capaldo

5/3/2005 1:23:00 AM

0

On 5/2/05, Phil Tomson <ptkwt@aracnet.com> 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?



Igor

9/15/2010 9:06:00 PM

0



Eddie Haskell wrote:
>You want to come here you will obey our rules
> and customs.

Yeah, like the First Amendment. Get that one thru your thick head,
freeper.


Eddie Haskell

9/15/2010 9:21:00 PM

0


<bmoore@nyx.net> wrote in message
news:33453dd5-99c1-4a4e-87f9-c1fbe0e0eaf8@t5g2000prd.googlegroups.com...
On Sep 14, 5:29 pm, "Eddie Haskell" <gaga...@gagagag.com> wrote:
> "O'Neil's Faggy Prostate - Watermelon with salt is very
> good."<oneilsprost...@aol.com> wrote in message
>
> news:8f0f1fee-efca-4095-86eb-22b4eec4941f@u13g2000vbo.googlegroups.com...
>
> > Good for them.
>
> I swear. When I read the subject header that was my very thought. You
> stole
> the very words right out of my mouth.
>
> >http://gatewaypundit.firstthings.com/2010/09/french-senate-......
>
> > "The French Senate has voted overwhelmingly for a bill banning the
> > burka-style Islamic veil everywhere from post offices to streets, in a
> > final step toward a making it law."
>
> > You wanna live in France, then behave in a fashion that is acceptable
> > in their society.
>
> > Pretty simple, really.
>
> Besides that. It's considered rude in western culture to wear shit
> covering
> up you face like that in public. It's like some dumbass wearing sunglasses
> or a hat indoors. Fuck 'em. You want to come here you will obey our rules
> and customs.
>
> -Eddie Haskell

> I'm pretty sure that shitting in public is considered rude, yet you do
> it all the time,

Yeah, take a shit all over democrats in this forum.

Glad you noticed.

-Eddie Haskell