Re: getting instance pointer in weak ref notify



On 13 Aug 2001, Owen Taylor wrote:

>
> James Henstridge <james daa com au> writes:
>
> > It would be very useful if the instance pointer value was passed as an
> > argument to weak ref notify function.  For the simple cases where you just
> > want to unref or destroy some other object/structure when the object is
> > finalized, the current code is okay.
> >
> > But there are many cases where knowing the instance pointer value would be
> > very useful.  A fairly common case is where you want to maintain a list or
> > hash table of all `live' objects of a particular type.  Ideally, you could
> > do something like this:
> >   void
> >   weak_notify (gpointer data, gpointer instance_pointer)
> >   {
> >     GHashTable *ht = data;
> >
> >     g_hash_table_remove (ht, instance_pointer);
> >   }
> >   ...
> >   g_object_weak_ref (object, weak_notify, ht);
> >
> > In this case, the instance pointer is only needed for its value -- it
> > isn't dereferenced.  Unfortunately, this sort of thing is pretty much
> > impossible with the current weak ref code (the current weak ref code
> > doesn't offer much above setting data with a destroy notify).
> >
> > I propose adding an extra "instance_pointer" parameter to the GWeakNotify
> > function type, so that the notify function can use the pointer value.  I
> > know that you don't want to make it easy for people to shoot themselves in
> > the foot, but leaving this out greatly reduces the usefulness of the API.
> >
> > Possible ways to decrease the likelyhood of foot shootings could include:
> >   1) call the instance_pointer parameter
> >      "instance_pointer_dont_ref_or_use".
> >   2) set the class pointer in the GObject to NULL while notifying weak
> >      refs, so any of the cast check macros would fail.  This puts the
> >      instance in an inconsistent state though.
> >   3) record the reference count before calling the weak notify function
> >      and comparing the saved value with the actual one after calling, and
> >      spit out a warning if they differ.
>
> On consideration, I think it's probably better in balance to add the
> objectf pointer back in. If people really want to shoot themselves
> in the foot, they'll do the same thing as they would to work around
> the limitation of not having the object * - create a structure
> holding the object and a pointer to the real data.
>
> (Or, hold a back pointer to the object from the real data).
>
> I don't think any of these safeguards are really that useful, and
> I think they are dangerous as well -- since we have premature
> notification of weak references (for, say, gtk_widget_destroy())
> it's perfectly reasonable for _other_ people than the weak reference
> holder to be manipulating the object at that point.
>
> So, the main question in my mind is:
>
>  typedef GWeakNotify (GObject *object, gpointer data);
>
> vs.
>
>  typedef GWeakNotify (gpointer data, GObject *object);

I would lean towards the second option.  Even though it differs from most
other GObject APIs, it will allow passing g_free or g_object_unref as weak
notifiers directly, rather than having to use an additional wrapper
function.

James.

-- 
Email: james daa com au
WWW:   http://www.daa.com.au/~james/






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