getting instance pointer in weak ref notify



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.

James.





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