[[PageOutline]] = Reference counting with `GRefPtr` = In the GLib/GObject/GTK+ object model, instances are reference-counted. To ease handling those in !WebKitGTK+ code, the `GRefPtr` template is provided. It keeps a pointer to a reference-counted and takes care of properly increasing and decreasing the reference counts for object being held. Helper functions are provided as well (e.g. `adoptGRef()`). Sources: * [source:trunk/Source/WTF/wtf/gobject/GRefPtr.h Source/WTF/wtf/gobject/GRefPtr.h] * [source:trunk/Source/WTF/wtf/gobject/GRefPtr.cpp Source/WTF/wtf/gobject/GRefPtr.h] == Usage == === Creation === Creating a `GRefPtr` initializes it to `nullptr`: {{{ #!cpp GRefPtr context; // Check always succeeds, nullptr is the default value. ASSERT(!context); }}} === Destruction === When a `GRefPtr` goes is destroyed, it decreases the reference count of the object it holds. === Adopting a reference === Sometimes it is desirable to store a pointer in a `GRefPtr` without increasing the reference counter. The main use case is assigning a newly created instance to a `GRefPtr`: the newly created instance already has a reference count of ''one'' and assigning it to a `GRefPtr` would increase the reference count to ''two'', which would be incorrect (and very likely would cause a leak). To avoid this, the `adoptGRef()` helper function is provided. This function creates a `GRefPtr` without increasing the reference counter of the object it holds. This is incorrect: {{{ #!cpp // Incorrect: The created object has *two* references, even // when referenced only from the “context” variable. GRefPtr context = g_main_context_new(); }}} And this is correct: {{{ #! // Correct: The newly created object has only one reference GRefPtr context = adoptGRef(g_main_context_new()); }}} === Assignment === Assigning to a `GRefPtr` increases the reference count for the pointer being assigned. Both raw pointers and other `GRefPtr` instances can be assigned: {{{ #!cpp void doWithWidget(GtkWidget* widget) { GRefPtr widgetLocal1 = widget; GRefPtr widgetLocal2 = widgetLocal1; } }}} === Raw pointer === To obtain the raw pointer use the `get()` method. This is typically used when calling functions from support libraries used by the port (GLib, GTK+, etc). For example: {{{ #!cpp void show(GRefPtr& widget) { gtk_widget_show_all(widget.get()); } }}} == Floating references == Instances of most types have a ''floating reference'' when created. Most `GObject`-derived types, but also some other types have floating references (`GVariant` is one of those). It is incorrect to use `adoptGRef()` on newly created instances of types which use floating references: the floating reference has to be converted on a full reference. Assigning to a `GRefPtr` will convert the floating reference to a full reference. Incorrect usage: {{{ #!cpp GRefPtr variant = adoptGRef(g_variant_new("s", "Incorrect usage")); }}} Correct usage: {{{ #!cpp GRefPtr variant = g_variant_new("s", "Floating reference is sank"); }}}