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;
}



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