# Question about G\_CHILD\_ADD/G\_CHILD\_REMOVE

**URL:** <https://www.ruby-forum.com/t/question-about-g-child-add-g-child-remove/164003>\
**Category:** Ruby-Gnome 2\
**Created:** [March 26, 2009, 7:40pm UTC](https://www.ruby-forum.com/t/question-about-g-child-add-g-child-remove/164003 "2009-03-26T19:40:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nikolai\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/df705f/32.png) [@Nikolai\_W](https://www.ruby-forum.com/u/Nikolai_W)\
**Post date:** [March 26, 2009, 7:40pm UTC](https://www.ruby-forum.com/t/question-about-g-child-add-g-child-remove/164003/1 "2009-03-26T19:40:27Z")

</div>

Am I right in understanding that if you have code like

[GFile.new(‘a’), GFile.new(‘b’).each do |file|  
file.read\_async{ |contents| p contents }  
end

will be fine even though #read\_async is using code like

G\_CHILD\_ADD(mGLib, block);  
…  
G\_CHILD\_REMOVE(mGLib, block);

where block is generated by rb\_block\_proc()? I mean, rb\_block\_proc()  
gives me a new proc for each invocation that has a unique hash, right?

Sorry about all these questions that probably have pretty obvious  
answers, but I’m really managing to confuse myself with this.

---

<div class="post-metadata">

**Author:** ![Nikolai\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/df705f/32.png) [@Nikolai\_W](https://www.ruby-forum.com/u/Nikolai_W)\
**Post date:** [March 27, 2009, 2:04pm UTC](https://www.ruby-forum.com/t/question-about-g-child-add-g-child-remove/164003/2 "2009-03-27T14:04:23Z")

</div>

Hi,

In [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
“[ruby-gnome2-devel-en] Question about G\_CHILD\_ADD/G\_CHILD\_REMOVE” on  
Thu, 26 Mar 2009 19:38:32 +0100,  
Nikolai W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> G\_CHILD\_REMOVE(mGLib, block);
> 
> where block is generated by rb\_block\_proc()? I mean, rb\_block\_proc()  
> gives me a new proc for each invocation that has a unique hash, right?

Yes. But please use G\_CHILD\_\* with an instance not  
mGLib. It’s enough that a callback block is associated with  
just an instance. Lifetimes of a callback block and an  
instance are same.

## Thanks,

kou

---

<div class="post-metadata">

**Author:** ![Nikolai\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/df705f/32.png) [@Nikolai\_W](https://www.ruby-forum.com/u/Nikolai_W)\
**Post date:** [March 27, 2009, 3:00pm UTC](https://www.ruby-forum.com/t/question-about-g-child-add-g-child-remove/164003/3 "2009-03-27T15:00:09Z")

</div>

On Fri, Mar 27, 2009 at 14:01, Kouhei S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > end  
> > Yes. But please use G\_CHILD\_\* with an instance not  
> > mGLib. It’s enough that a callback block is associated with  
> > just an instance. Lifetimes of a callback block and an  
> > instance are same.

Yes, that was my initial thought as well. However, doing that will  
complicate the code quite a bit. At the moment I simply pass the  
block as the user\_data to the async functions. If I associate it with  
self I’ll have to pass in an allocated struct (or similar) containing  
both self and the block, right?

Or can I do something like the following?

static void  
read\_async\_callback(GObject \*source, GAsyncResult \*result, gpointer  
data)  
{  
VALUE block = (VALUE)data;  
â‹®  
if (NIL\_P(block))  
return;

G\_CHILD\_REMOVE(GOBJ2RVAL(source), block);  
rb\_funcall(block, g\_id\_call, â€¦);  
}

static VALUE  
read\_async(int argc, VALUE \*argv, VALUE self)  
{  
â‹®  
VALUE block;  
â‹®  
rb\_scan\_args(argc, argv, “…&”, …, &block);  
if (!NIL\_P(block))  
G\_CHILD\_ADD(self, block);  
â‹®  
g\_file\_read\_async(\_SELF(self), …, read\_async\_callback, &block);  
â‹®  
}

That is, will self and GOBJ2RVAL(source) be the same VALUE? If I  
understand GOBJ2RVAL correctly they won’t. Is there a way of getting  
“self” in read\_async\_callback?

(I know that \_SELF(self) and source are the same.)

Thanks.
