Re: Reference Counting
Reference counting for tree models doesn't behave the same way as for contained components. The tree view adds an additional reference to the model, so in your case your model is never destroyed as the ref count is 2. Try invoking: g_object_unref(model) after you add it to the view. My source for this info is: http://scentric.net/tutorial/sec-treeview.html#sec-treeview-connect-model-refcounting Haven't tried it. Matias Torres wrote: ABOUT WEAK_REFERENCES I can't get g_object_weak_ref working, this is the (full) example I've tried it on. It tryes to free a GDate which is in a gtktreemodel. // #include gtk/gtk.h #include glib.h #include stdio.h void weak_ref (gpointer data, GObject *object) { g_date_free (data); printf (weak_ref\n); } GtkTreeModel *generate_tree_model () { GtkListStore *model; GtkTreeIter iter; GDate *date = g_date_new_dmy (10, 4, 1986); model = gtk_list_store_new ( 3, G_TYPE_INT, G_TYPE_STRING, G_TYPE_POINTER, -1); gtk_list_store_append (model, iter); gtk_list_store_set (model, iter, 0, 1, 1, Hol, 2, date, -1); g_object_weak_ref (G_OBJECT(model), (GWeakNotify) weak_ref, date); date = g_date_new_dmy (10, 4, 1986); gtk_list_store_append (model, iter); gtk_list_store_set (model, iter, 0, 2, 1, Chauuu, 2, date, -1); g_object_weak_ref (G_OBJECT(model), (GWeakNotify) weak_ref, date); return GTK_TREE_MODEL (model); } GtkWidget *generate_tree_view () { GtkTreeView *treeview; GtkCellRenderer *renderer; GtkTreeViewColumn *column; treeview = GTK_TREE_VIEW (gtk_tree_view_new ()); renderer = gtk_cell_renderer_text_new (); column = gtk_tree_view_column_new_with_attributes (UNO, renderer, text, 0, NULL); gtk_tree_view_append_column (treeview, column); renderer = gtk_cell_renderer_text_new (); column = gtk_tree_view_column_new_with_attributes (DOS, renderer, text, 1, NULL); gtk_tree_view_append_column (treeview, column); return GTK_WIDGET (treeview); } int main (int argc, char **argv) { GtkWidget *window; GtkWidget *treeview; gtk_init (argc, argv); window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_title (GTK_WINDOW (window), Reference Counting); treeview = generate_tree_view (); gtk_tree_view_set_model (GTK_TREE_VIEW (treeview), generate_tree_model ()); gtk_container_add (GTK_CONTAINER (window), treeview); g_signal_connect (G_OBJECT (window), delete-event, G_CALLBACK (gtk_main_quit), NULL); gtk_widget_show_all (window); gtk_main (); return 0; } It NEVER prints weak ref. ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Re: Reference Counting
Have you tried a minimal example? Unfortunately I don't have access to gtk-2.0 here, but here is a 1.2 sample that works: #include stdio.h #include stdlib.h #include gtk/gtkobject.h #include gtk/gtkentry.h static void weak_notify_test(gpointer the_data, GtkObject* o); static void weak_notify_test(gpointer the_data, GtkObject* o){ int *intarray = (int *) the_data; printf(The array: %d %d\n, intarray[0], intarray[1]); free(the_data); } int main(int argc, char** argv){ GtkEntry* entry; int* intarray; gtk_init(argc, argv); entry = gtk_entry_new (); intarray = malloc(2 * sizeof(int)); intarray[0] = 1; intarray[1] = 2; gtk_object_weakref (GTK_OBJECT(entry), (GtkDestroyNotify)weak_notify_test, intarray); gtk_object_unref(entry); return 0; } g_object_weak_ref again? g_object_weak_ref should do. Thanks anyway! Matias Nikhil Dinesh escribió: *- Is there a way to pass a function to free certain struct in a GtkTreeModel so GTK frees the allocated space when the model's reference count reach to 0? (without registering it as a G_TYPE).* One way is to register the struct as a weak reference to the model, using g_object_weak_ref. See: http://developer.gnome.org/doc/API/2.0/gobject/gobject-The-Base-Object-Type.html *- When reference count starts? I read somewhere that it starts when you add the widget into a container, is it always this way??? Eg:* Reference counting starts when an object is created. In your example: GtkWidget *entry = gtk_entry_new (); After this statement, there is one reference to the entry. When gtk_container_add is invoked, it takes over ownership of this reference. That is when the container is destroyed, g_object_unref is invoked on the entry. So one needs to invoke destroy only on the root widget (typically). //*No reference counting on GtkEntry*// GtkWidget *entry = gtk_entry_new (); //*Reference counting starts here *// gtk_container_add (GTK_CONTAINER (window), entry);/ /* Another doubt: in this case, reference couting starts with 1 or 2? */ / -* If i get a string from a GtkTreeModel, it gives me a copy or the actual pointer to the data? If you're using the get_value method on the iterator, the object is copied into the GValue supplied. Typically this gives you just a reference, and invoking g_value_unset results in just decrementing the reference count. But what exactly happens during copying depends on your object. See: http://developer.gnome.org/doc/API/2.0/gobject/gobject-Generic-values.html *That's it, thanks in advance. Matias. ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Re: Reference Counting
*- Is there a way to pass a function to free certain struct in a GtkTreeModel so GTK frees the allocated space when the model's reference count reach to 0? (without registering it as a G_TYPE).* One way is to register the struct as a weak reference to the model, using g_object_weak_ref. See: http://developer.gnome.org/doc/API/2.0/gobject/gobject-The-Base-Object-Type.html *- When reference count starts? I read somewhere that it starts when you add the widget into a container, is it always this way??? Eg:* Reference counting starts when an object is created. In your example: GtkWidget *entry = gtk_entry_new (); After this statement, there is one reference to the entry. When gtk_container_add is invoked, it takes over ownership of this reference. That is when the container is destroyed, g_object_unref is invoked on the entry. So one needs to invoke destroy only on the root widget (typically). //*No reference counting on GtkEntry*// GtkWidget *entry = gtk_entry_new (); //*Reference counting starts here *// gtk_container_add (GTK_CONTAINER (window), entry);/ /* Another doubt: in this case, reference couting starts with 1 or 2? */ / -* If i get a string from a GtkTreeModel, it gives me a copy or the actual pointer to the data? If you're using the get_value method on the iterator, the object is copied into the GValue supplied. Typically this gives you just a reference, and invoking g_value_unset results in just decrementing the reference count. But what exactly happens during copying depends on your object. See: http://developer.gnome.org/doc/API/2.0/gobject/gobject-Generic-values.html *That's it, thanks in advance. Matias. ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Virtual functions and chaining up
Hi, I'm trying to create my own object hierarchy, and I don't understand the part in the API reference about virtual functions and chaining up. To create a virtual public method, the docs say we need to do something like: /* declaration in maman-bar.h. */ struct _MamanBarClass { GObjectClass parent; /* stuff */ void (*do_action) (MamanBar *self, /* parameters */); }; void maman_bar_do_action (MamanBar *self, /* parameters */); /* implementation in maman-bar.c */ void maman_bar_do_action (MamanBar *self, /* parameters */) { MAMAN_BAR_GET_CLASS (self)-do_action (self, /* parameters */); } I assume that the subclass definition looks like: struct _MamanBarSubClass { MamanBarClass parent; void (*do_action) (MamanBarSub* self, /* params */); } If the maman_bar_do_action actually needs to do something, and the derived class needs to chain up, wouldn't it result in another invocation of maman_bar_do_action which is again redirected to the derived class? This would of course result in an infinite loop. Please clarify. Thanks, -Nikhil ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list