On Fri, 2010-01-22 at 15:13 +0100, fka...@googlemail.com wrote:
> So, could anyone please confirm if this is correctly coded:

Well, first of all I don't think g_object_force_floating() is intended
for general application use. Beside that, the code looks overly
complicated. Why not do:

(0) Object creation / adding to container

child = gtk_some_widget_new (some_params);
g_object_ref_sink (G_OBJECT (child);
gtk_container_add (GTK_CONTAINER (container), child);


> (1) To remove a child for possible later usage I do:

> gtk_container_remove(
>   GTK_CONTAINER(container),GTK_WIDGET(child));


> (2) To later re-pack the child

> gtk_box_pack_start/end(
>   GTK_BOX(container), child, ...);


> (3) Before the child is destroyed (app closed) I do: */

> g_object_unref( G_OBJECT(child) );


Some explanations:
(0)
  - child is initially created with a floating reference.
  - we take ownership of the floating reference
  - gtk_container_add() calls g_object_ref_sink internally - as
    child no longer is floating, this behaves like g_object_ref()
 [side node: the calls to g_object_ref_sink() and gtk_container_add()
             can be in reverse order as well]

(1)
  - we just remove child from the container - this decreases the
    ref_count back to 1 (the reference we hold with child)

(2)
  - we (re)add the widget to the container - this increments the
    ref_count of child, so it's back to 2

(3)
  - we call g_object_unref, which decreases the ref_count once
    again; when the container is destroyed, g_object_unref() is
    called on child, so ref_count drops to 0

There's really no use in trying to keep the ref_count at 1 - after all,
it just boils down to some integer in a struct. As long as you call
g_object_unref() for every reference you hold, you are fine.

Hope that clears things up a little - happy hacking!
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Reply via email to