Re: GWeakNotify fired earlier than expected



After reading the gobject code a bit more, it seems that weak
references are fired on dispose, not finalize:

static void
g_object_real_dispose (GObject *object)
{
  g_signal_handlers_destroy (object);
  g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
  g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
}

All of the documentation I could find states that it happens during
finalization, but it seems it should all say that it happens during
dispose.  In my example this detail is actually very important.
Doesn't look like there is a way to catch dispose without shimming the
instance handler.  Any ideas?

On Mon, Mar 16, 2009 at 5:16 PM, IdaRub <idarub gmail com> wrote:
Hi,

From reading the documentation, I am expecting a GWeakNotify to be
fired when the object is finalized (which I interpret as right before
it is freed).  However, I am seeing it called during destroy while
references are still held.  I am likely just misunderstanding
something, any explanations?  Thanks...

$ ./weak
created widget, ref count 1
in container, ref count 2
Finalize, ref count 2
destroyed container 1
destroying last ref 1


$ cat weak.cc
#include <stdio.h>
#include <gtk/gtk.h>

static void OnFinalizeDebug(gpointer userdata, GObject* wasptr) {
 printf("Finalize, ref count %d\n", wasptr->ref_count);
}

int main(int argc, char** argv) {
 gtk_init(&argc, &argv);

 GtkWidget* widget = gtk_label_new("abc");
 g_object_ref_sink(widget);
 g_object_weak_ref(G_OBJECT(widget), OnFinalizeDebug, &widget);
 printf("created widget, ref count %d\n", G_OBJECT(widget)->ref_count);

 GtkWidget* box = gtk_vbox_new(FALSE, 0);
 gtk_container_add(GTK_CONTAINER(box), widget);
 printf("in container, ref count %d\n", G_OBJECT(widget)->ref_count);

 gtk_widget_destroy(box);
 printf("destroyed container %d\n", G_OBJECT(widget)->ref_count);

 printf("destroying last ref %d\n", G_OBJECT(widget)->ref_count);
 g_object_unref(widget);

 return 0;
}




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