Re: Introducing "toggle references"



Hi Owen,

On Mon, 2005-04-25 at 16:40 -0400, Owen Taylor wrote:

> The basic idea of the scheme is that when the reference count on the
> GObject drops to 1, we know that the only reference left to the
> GObject is held by the proxy. So, there is no reason that the GObject
> should keep the proxy alive, and we convert the reference from
> GObject to the proxy to a weak reference. 
> 
> We go from:
>                             |
>   Other object ---> Proxy <--- GObject <--- Other Gobject 
>                            --->
>                             |
> To:
> 
>                             |
>   Other object ---> Proxy <... GObject 
>                            --->
>                             |
> 
> If all other references to the Proxy are dropped, then we end up 
> with:
>                             |
>                     Proxy <... GObject 
>                            --->
>                             |
> 
> The Proxy is garbage collected, and at that point the reference count
> on the GObject drops to zero and it is freed as well.

	Just to be clear, would this look like:

static void
weak_ref_notify (gpointer  data,
                 GObject  *gobject)
{
  FooObject *object = (FooObject *) data;
  FooProxy  *proxy = (FooProxy *) gobject;

  g_object_set_data (object, "foo-proxy", NULL);
}
                 
static void
toggle_notify (gpointer  data,
               GObject  *gobject,
               gboolean  is_last_ref)
{
  FooProxy  *proxy = (FooProxy *) data;
  FooObject *object = (FooObject *) gobject;

  if (last_ref)
    {
      g_object_weak_ref (proxy, weak_ref_notify, object);
      g_object_unref (proxy);
    }
  else
    {
      g_object_ref (proxy);
      g_object_weak_unref (proxy, weak_ref_notify, object);
    }
}

void
foo_proxy_bind_to_object (FooProxy  *proxy,
                          FooObject *object)
{
  gobject_add_toggle_ref (object, toggle_notify, proxy);
  proxy->object = object;

  g_object_set_data (object, "foo-proxy", g_object_ref (proxy));
}

static void
foo_proxy_finalize (FooProxy *proxy)
{
  if (proxy->object != NULL)
    g_object_unref (proxy->object);
  proxy->object = NULL;
}

	If so, then what's really hurting my brain is the fact that the
reference add_toggle_ref() takes isn't the reference which toggles
between weak and strong; its actually the reference from the object to
the proxy which toggles.

	Also, would it not make sense for add_toggle_ref() to return the
reference?

	I guess explicitly maintaining the weak reference from the object to
the proxy is fairly superflous in this example too ...

Cheers,
Mark.




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