-
Pl
chevron_right
Michael Catanzaro: Your _get_type() function is not G_GNUC_CONST: Part Two
news.movim.eu / PlanetGnome • 13 hours ago • 1 minute
This blog post is a sequel to Your _get_type() function is not G_GNUC_CONST .
GNOME developers have long used
G_GNUC_CONST
, which expands to
__attribute__((const))
, to annotate GObject
_get_type()
functions, despite knowing that it is incorrect to do so. const functions by definition have no side effects, but
_get_type()
functions actually have a side effect the first time the function is called: they initialize the type. Why apply an incorrect annotation to these functions? Because it makes the code faster.
Although this was
long known to be incorrect
, it worked fine in practice… until now. Regrettably, Sam James has discovered that
GCC 16 may optimize away the type initialization
, resulting in crashes. This is our fault for providing the compiler with wrong information about our code, so it’s time to audit your use of const attributes to remove them from
_get_type()
functions. Most GNOME programs use these attributes
only
for
_get_type()
functions, but if you use it in more places, then check to make sure those functions are actually const, as defined by the
GCC documentation
.
Sadly, there is no suitable replacement attribute for
_get_type()
functions. Two decades ago,
Behdad requested a new idempotent attribute
for expressing the desired semantics, but nobody has implemented it.