Consider adding g_weak_ref_new/free()
@xclaesse
Submitted by Xavier Claessens Link to original bug (#680813)
Description
I think it is a common use case to have a GObject as user_data of an async call, and you can't always cancel the call when the object dies. You don't always want to make the object survive until the operation finish.
I would like to do something like that:
my_callback (GObject *source, GAsyncResult *result, gpointer user_data) { GWeakRef *weak = user_data; MyFoo *self = g_weak_ref_get (weak);
if (self == NULL) goto out;
...
g_object_unref (self);
out: g_weak_ref_free (weak); }
my_foo_async (self->priv->something, my_callback, g_weak_ref_new (self));
We have TpWeakRef in telepathy-glib library for exacly that usecase. It has the convenience to have an extra gpointer user_data to be able to pass 2 stuff to the callback.
Or maybe we could add a new typedef struct {GWeakRef weak; gpointer user_data; GDestroyNotify destroy;} GWeakData; ?