Changes between Initial Version and Version 1 of WebKitGTK/GRefPtrAndFloatingRefs

Feb 18, 2016 11:14:53 PM (6 years ago)
Carlos Garcia Campos



  • WebKitGTK/GRefPtrAndFloatingRefs

    v1 v1  
     1= GRefPtr and floating references =
     3g_object_new() returns a floating reference for GObjects derived from GInitiallyUnowned. Those are expected to be consumed (converted to a normal reference) when passed to another class. So, for example, GtkWidgets are all GInitiallyUnowned, since in most of the cases you create a widget and then add it to a container. We don't need to care about widgets refcounting in this case because the container is consuming the floating reference and then taking the ownership of the widget.
     5GRefPtr always uses g_object_ref_sink() for taking references, so that if the object has a floating reference it will be consumed, and if not, the reference counter will be increased in 1 as expected. We should only use GRefPtr with GInitiallyUnowned objects when we actually want to consume the reference, or if we want to prevent the object from being leaked in case of an early return before someone else has consumed the floating reference. That's why adoptGRef should never be used with GInitiallyUnowned, we either consume the reference or don't use GRefPtr at all.
     7In summary:
     9* If we know the floating reference is going to be consumed, and there's not risk of leaking, we use pointers. For example:
     12button = gtk_button_new();
     13gtk_container_add(container, button);
     16* If we know the floating reference is not going to be consumed, we use GRefPtr without adopting the reference at all. For example:
     19GRefPtr<GVariant> variant = g_variant_new("i", 25);
     20char* serializeVariant = g_variant_print(varaint.get(), TRUE);
     23* If the floating reference might be consumed, but there's any early return that could leak the reference we use GRefPtr without adopting the reference. For example:
     26GRefPtr<WebKitWebView> webView = webkit_web_view_new();
     27if (cancelled)
     28    return;
     30gtk_continer_add(window, webView.get());
     33Of course we should try to avoid this case, and always create the object right before it is going to be added to a container and use plain pointers in that case.
     35So, why is adoptGRef() so dangerous for GInitiallyUnowned objects? Let's use the last example. If we adopt the WebKitWebView reference and we don't return early, after the gtk_continer_add() the GObject will still have 1 reference, but both the GRrfPtr and the GtkWindow will call g_object_unref().