Re: GIOChannels and refcounts.



Harring Figueiredo wrote:
--- Tristan Van Berkom <vantr touchtunes com> wrote:

Hmmm,


g_io_channel_unix_new(fd);                   // created with refcount 1
source = g_io_channel_add_watch(channel...); // refcount is now 2

   Don't think this increments the ref_count. (At least I could not see it on
the code. glib 2.4.0 )

The doc at:
    http://developer.gnome.org/doc/API/2.0/glib/glib-IO-Channels.html
says:
===============================================================
GIOChannel instances are created with an initial reference count of 1. g_io_channel_ref() and g_io_channel_unref() can be used to increment or decrement the reference count respectively. When the reference count falls to 0, the GIOChannel is freed. (Though it isn't closed automatically, unless it was created using g_io_channel_new_from_file().) Using g_io_add_watch() or g_io_add_watch_full() increments a channel's reference count.
===============================================================

/* ... readjust some stuff, now I dont need this channel ... */
g_source_remove(source);              // does this decrement io channels
                                      // refcount ?
  Again, ref_count not touched.

Sorry, I was curious and checked for myself:

giounix.c
==================================
/*
...
 */
GSourceFuncs g_io_watch_funcs = {
  g_io_unix_prepare,
  g_io_unix_check,
  g_io_unix_dispatch,
  g_io_unix_finalize
};
/*
...
 */
static void
g_io_unix_finalize (GSource *source)
{
  GIOUnixWatch *watch = (GIOUnixWatch *)source;

  g_io_channel_unref (watch->channel);
}
=====================================

So when you g_source_remove, you effectivly remove the refcount
added by g_io_add_watch.

All is good ;-)

Cheers,
                                         -Tristan




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]