g_object_ref() now propagates types



Hi all,

We just landed a patch in GLib which propagates the type from the
argument of g_object_ref() to its return type:

https://git.gnome.org/browse/glib/commit/?id=3fae39a5d
https://bugzilla.gnome.org/show_bug.cgi?id=790697

The idea here is that it will catch invalid implicit casts which the
compiler otherwise wouldn’t have noticed because g_object_ref()
previously operated entirely on gpointers. This will eliminate a whole
class of bugs: bugs which are unlikely to exist, but are a complete
pain to track down if they do.

The downside of this is that some legitimate implicit casts may now
cause compiler warnings with -Wincompatible-pointer-types. For example,
in the situation below, a warning will be introduced since parent_type
isn’t guaranteed to be an instance of child_type:

ParentType *parent_type;
ChildType *child_type;

child_type = g_object_ref (parent_type);

To fix this warning, first double-check that parent_type is actually
guaranteed to always be an instance of child_type at runtime, and then
change the ref to:

child_type = CHILD_TYPE (g_object_ref (parent_type));

That will add a compile-time explicit cast, and a runtime type check.
(As always, the runtime type check is disabled if GLib is built without
debugging enabled (or with G_DISABLE_CAST_CHECKS defined.)

Note that the new behaviour requires GCC, and is only enabled if you
have defined GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_56.

If anybody encounters any problems with this, please comment on the bug
report:

https://bugzilla.gnome.org/show_bug.cgi?id=790697

Thanks,
Philip
on behalf of the GLib maintainership cabal

Attachment: signature.asc
Description: This is a digitally signed message part



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]