When should I use g_object_unref()

You should also check the is_floating() state of a widget:

When you create a new widget like a button its state should be floating, and generally ref count is 1. Note that ref_count is called a private field of gobject, so it is not really intended for inspection. If the widget is floating, and you add it to a container, than state should go to none floating. So the container takes ownership, you do not have to call g_object_unref() on it. But you would be free to call g_object_ref() g_object_unref() pairs on the object, maybe to keep it alive, for example when you temporary remove it from a container and later want to add it again.

The actual number of ref_count does not really matter, maybe when you add a button to a window that window references that widget multiple time – window is free to do that. What you may test is ref the button, add the button to the window and remove it again from the window. Then the effect of the window should cancel out, and ref count should be again 1 I guess.

I was indeed surprised by some ref count values myself when working on the Nim bindings, but finally I cared only for the 1/0 transition for the destroy process.

1 Like