anton_rh
anton_rh

Reputation: 9223

Should we always use g_object_ref_sink instead of g_object_ref?

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

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?

Upvotes: 1

Views: 757

Answers (1)

Philip Withnall
Philip Withnall

Reputation: 5768

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.

Upvotes: 1

Related Questions