Re: GWeakNotify fired earlier than expected
Hi, On Mon, Mar 16, 2009 at 12:45 PM, IdaRub ida...@gmail.com wrote: After reading the gobject code a bit more, it seems that weak references are fired on dispose, not finalize: Right, that's correct. 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. Probably true. dispose is typically done during finalize, but can happen sooner also. dispose can happen multiple times. 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? You mean there's no way to catch finalize? Finalize deliberately can't be intercepted, because it raises a lot of thorny issues about reentrancy; what if the object is used or its refcount increased during finalization? So things are split into two phases, dispose() which you can get notifications about (and which can happen multiple times); and finalize which actually frees the object. Havoc ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
GWeakNotify fired earlier than expected
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; } ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
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 ida...@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; } ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Re: GWeakNotify fired earlier than expected
On Mon, Mar 16, 2009 at 5:51 PM, Havoc Pennington havoc.penning...@gmail.com wrote: Hi, On Mon, Mar 16, 2009 at 12:45 PM, IdaRub ida...@gmail.com wrote: After reading the gobject code a bit more, it seems that weak references are fired on dispose, not finalize: Right, that's correct. 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. Probably true. dispose is typically done during finalize, but can happen sooner also. dispose can happen multiple times. 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? You mean there's no way to catch finalize? Finalize deliberately can't be intercepted, because it raises a lot of thorny issues about reentrancy; what if the object is used or its refcount increased during finalization? It would be nice if you could intercept it, the author being responsible for understanding what finalize means, and not doing anything inappropriate. I was attempting to write some ownership debugging code, basically something like leak and use-after-free detection for a few specific object. We'll catch this other ways though, so I suppose I will drop this idea. Thanks for your great reply. So things are split into two phases, dispose() which you can get notifications about (and which can happen multiple times); and finalize which actually frees the object. Havoc ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
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 ida...@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; } ___ gtk-devel-list mailing list gtk-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-devel-list
Re: GWeakNotify fired earlier than expected
Hi, On Mon, Mar 16, 2009 at 12:45 PM, IdaRub ida...@gmail.com wrote: After reading the gobject code a bit more, it seems that weak references are fired on dispose, not finalize: Right, that's correct. 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. Probably true. dispose is typically done during finalize, but can happen sooner also. dispose can happen multiple times. 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? You mean there's no way to catch finalize? Finalize deliberately can't be intercepted, because it raises a lot of thorny issues about reentrancy; what if the object is used or its refcount increased during finalization? So things are split into two phases, dispose() which you can get notifications about (and which can happen multiple times); and finalize which actually frees the object. Havoc ___ gtk-devel-list mailing list gtk-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-devel-list
Re: GWeakNotify fired earlier than expected
On Mon, Mar 16, 2009 at 5:51 PM, Havoc Pennington havoc.penning...@gmail.com wrote: Hi, On Mon, Mar 16, 2009 at 12:45 PM, IdaRub ida...@gmail.com wrote: After reading the gobject code a bit more, it seems that weak references are fired on dispose, not finalize: Right, that's correct. 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. Probably true. dispose is typically done during finalize, but can happen sooner also. dispose can happen multiple times. 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? You mean there's no way to catch finalize? Finalize deliberately can't be intercepted, because it raises a lot of thorny issues about reentrancy; what if the object is used or its refcount increased during finalization? It would be nice if you could intercept it, the author being responsible for understanding what finalize means, and not doing anything inappropriate. I was attempting to write some ownership debugging code, basically something like leak and use-after-free detection for a few specific object. We'll catch this other ways though, so I suppose I will drop this idea. Thanks for your great reply. So things are split into two phases, dispose() which you can get notifications about (and which can happen multiple times); and finalize which actually frees the object. Havoc ___ gtk-devel-list mailing list gtk-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-devel-list