g_object_ref() now propagates types

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

g_object_ref() now propagates types

Philip Withnall-2
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
_______________________________________________
gtk-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-devel-list

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: g_object_ref() now propagates types

Emmanuele Bassi
On 8 December 2017 at 11:26, Philip Withnall <[hidden email]> wrote:


> If anybody encounters any problems with this, please comment on the bug
> report:
>
> https://bugzilla.gnome.org/show_bug.cgi?id=790697

As a side note: I've started a full Continuous rebuild, so if there
are projects that indeed break with this change, we should be notified
pretty quickly.

Ciao,
 Emmanuele.

--
https://www.bassi.io
[@] ebassi [@gmail.com]
_______________________________________________
gtk-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: g_object_ref() now propagates types

Paul Davis
In reply to this post by Philip Withnall-2


On Fri, Dec 8, 2017 at 6:26 AM, Philip Withnall <[hidden email]> wrote:
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.

​Great work. Another 3 or 4 or 200 of these and we'll almost have a C++ compiler!

tongue firmly and warmly in cheek :))) ​


_______________________________________________
gtk-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: g_object_ref() now propagates types

Sébastien Wilmet-2
In reply to this post by Philip Withnall-2
On Fri, Dec 08, 2017 at 11:26:31AM +0000, Philip Withnall wrote:
> Note that the new behaviour requires GCC, and is only enabled if you
> have defined GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_56.

If GLIB_VERSION_MAX_ALLOWED is not defined, does it take by default the
value of the last GLib version?

--
Sébastien
_______________________________________________
gtk-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: g_object_ref() now propagates types

Philip Withnall-2
On Sat, 2017-12-09 at 10:54 +0100, Sébastien Wilmet wrote:
> On Fri, Dec 08, 2017 at 11:26:31AM +0000, Philip Withnall wrote:
> > Note that the new behaviour requires GCC, and is only enabled if
> > you
> > have defined GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_56.
>
> If GLIB_VERSION_MAX_ALLOWED is not defined, does it take by default
> the
> value of the last GLib version?

That’s a good point. If left undefined, GLIB_VERSION_MAX_ALLOWED is
defined to the current stable release (GLIB_VERSION_CUR_STABLE). Once
2.56 is released, that means anyone who has not defined
GLIB_VERSION_MAX_ALLOWED will get the new g_object_ref() behaviour by
default.

Philip
_______________________________________________
gtk-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-devel-list

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: g_object_ref() now propagates types

Owen Taylor
In reply to this post by Philip Withnall-2
On Fri, Dec 8, 2017 at 6:26 AM, Philip Withnall <[hidden email]> wrote:

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.)

G_DISABLE_CAST_CHECKS is defined internally in GLib and GTK+ for stable releases (--enable-debug defaults to minimum for stable releases, yes for unstable releases), but that doesn't have any effect on whether they are enabled or disabled for your application; if you want to disable cast checks in production builds for your application, you need to specify G_DISABLE_CAST_CHECKS yourself.

Owen


_______________________________________________
gtk-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-devel-list