Forum: Ruby Saving Proc object in C extension

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2c7c807a1df0c76a8fc823c709b501a9?d=identicon&s=25 Victor Shepelev (Guest)
on 2006-03-26 16:42
(Received via mailing list)
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.
956f185be9eac1760a2a54e287c4c844?d=identicon&s=25 ts (Guest)
on 2006-03-26 16:54
(Received via mailing list)
>>>>> "V" == Victor Shepelev <vshepelev@imho.com.ua> 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
2c7c807a1df0c76a8fc823c709b501a9?d=identicon&s=25 Victor Shepelev (Guest)
on 2006-03-26 17:04
(Received via mailing list)
>
> 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.
This topic is locked and can not be replied to.