mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-12-27 07:56:14 +01:00
Merge branch 'floating-ref-warning' into 'master'
Document the best practices for binding GInitiallyUnowned See merge request GNOME/glib!836
This commit is contained in:
commit
f7a41aa43b
@ -53,6 +53,13 @@
|
||||
*
|
||||
* ## Floating references # {#floating-ref}
|
||||
*
|
||||
* **Note**: Floating references are a C convenience API and should not be
|
||||
* used in modern GObject code. Language bindings in particular find the
|
||||
* concept highly problematic, as floating references are not identifiable
|
||||
* through annotations, and neither are deviations from the floating reference
|
||||
* behavior, like types that inherit from #GInitiallyUnowned and still return
|
||||
* a full reference from g_object_new().
|
||||
*
|
||||
* GInitiallyUnowned is derived from GObject. The only difference between
|
||||
* the two is that the initial reference of a GInitiallyUnowned is flagged
|
||||
* as a "floating" reference. This means that it is not specifically
|
||||
@ -83,7 +90,23 @@
|
||||
* Since floating references are useful almost exclusively for C convenience,
|
||||
* language bindings that provide automated reference and memory ownership
|
||||
* maintenance (such as smart pointers or garbage collection) should not
|
||||
* expose floating references in their API.
|
||||
* expose floating references in their API. The best practice for handling
|
||||
* types that have initially floating references is to immediately sink those
|
||||
* references after g_object_new() returns, by checking if the #GType
|
||||
* inherits from #GInitiallyUnowned. For instance:
|
||||
*
|
||||
* |[<!-- language="C" -->
|
||||
* GObject *res = g_object_new_with_properties (gtype,
|
||||
* n_props,
|
||||
* prop_names,
|
||||
* prop_values);
|
||||
*
|
||||
* // or: if (g_type_is_a (gtype, G_TYPE_INITIALLY_UNOWNED))
|
||||
* if (G_IS_INITIALLY_UNOWNED (res))
|
||||
* g_object_ref_sink (res);
|
||||
*
|
||||
* return res;
|
||||
* ]|
|
||||
*
|
||||
* Some object implementations may need to save an objects floating state
|
||||
* across certain code portions (an example is #GtkMenu), to achieve this,
|
||||
|
Loading…
Reference in New Issue
Block a user