Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Saving Proc object in C extension

Reply
Thread Tools

Saving Proc object in C extension

 
 
Victor Shepelev
Guest
Posts: n/a
 
      03-26-2006
Hi all.

The question is: can I save some Proc object in global variable of C
extension?

What I've tried:

static VALUE g_callback;

//some callback-saving function:
static VALUE save_callback(VALUE self, VALUE proc)
{
...bla-bla-bla...

rb_global_variable(&proc); //as recommended in "Extending Ruby"

g_callback = proc;

...bla-bla-bla...
}

But when I've tried to use the saved g_callback later, I've received various
errors. Moreover,

rb_p(g_callback);

prints some garbage or crashes (just like object g_callback references is
deleted). Raw integer value of g_callback remains unchanged.

What I do wrong?

Thanks.

Victor.



 
Reply With Quote
 
 
 
 
ts
Guest
Posts: n/a
 
      03-26-2006
>>>>> "V" == Victor Shepelev <> writes:

V> static VALUE save_callback(VALUE self, VALUE proc)
V> {
V> ...bla-bla-bla...

V> rb_global_variable(&proc); //as recommended in "Extending Ruby"

proc is on the stack, after the end of save_callback() &proc don't make
reference to the proc object.

V> What I do wrong?

This is g_callback which must be registered.

static VALUE g_callback;

static VALUE save_callback(VALUE self, VALUE proc)
{
/* ... */
g_callback = proc;
/* ... */
}

/* ... */

void Init_bla_bla_bla()
{
/* ... */
g_callback = Qnil;
rb_global_variable(&g_callback);
/* ... */
}


Guy Decoux


 
Reply With Quote
 
 
 
 
Victor Shepelev
Guest
Posts: n/a
 
      03-26-2006
> >>>>> "V" == Victor Shepelev <> writes:
>
> V> static VALUE save_callback(VALUE self, VALUE proc)
> V> {
> V> ...bla-bla-bla...
>
> V> rb_global_variable(&proc); //as recommended in "Extending Ruby"
>
> proc is on the stack, after the end of save_callback() &proc don't make
> reference to the proc object.
>
> V> What I do wrong?
>
> This is g_callback which must be registered.


Thanks Guy! I've expected rb_global_variable uses only value of proc, not
address. Now I've understand my error.

Thanks!

> Guy Decoux


Victor.



 
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
Proc vs lambda vs proc Minkoo Seo Ruby 19 02-06-2007 11:13 AM
proc A def/calls proc B: variable scoping rules. NevilleDNZ Python 9 08-16-2006 04:36 AM
Convert VB.NET to TSQL PROC & Reference a Proc from another Proc David Lozzi ASP .Net 3 06-01-2005 06:35 PM
Why no Proc##[]=() ? Why no Proc##replace() ? Jean-Hugues ROBERT Ruby 14 05-05-2004 01:20 PM
What is the diff btwn 'sho proc' and 'sho proc cpu' William J King Cisco 1 12-18-2003 11:50 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57