Should we always use g_object_ref_sink instead of g_object_ref?

Viewed 294

There are two functions in GLib that work with the reference counting of the GObject objects:

  • g_object_ref increases reference count of the object, doesn't handle floating references.
  • g_object_ref_sink increases reference count of the object or just removes floating flag from the object if the reference is floating.

Since we don't know if the object is floating or not, we should always use g_object_ref_sink, shouldn't we? If I am wrong, when should we use g_object_ref and when should we use g_object_ref_sink? Should we use g_object_ref_sink only for GInitiallyUnowned objects?

1 Answers

You should generally know the type of an object you’re handling (i.e. pointers typically have more specific types than GObject*), so you’ll know if it’s potentially floating or not. GObject-based APIs which use floating references are documented as using them. Anything which isn’t documented as using floating references, doesn’t.

Related