Memory corruption and crash depending on the value at which GLIB_VERSION_MAX_ALLOWED is set
GLib 2.74.2 on Arch Linux
After searching for a while for the source of a memory corruption causing a crash of Xfburn, I realized that removing the definition of GLIB_VERSION_MAX_ALLOWED
, or requiring a sufficiently recent version of GLib, solved the problem.
Indeed, this component currently requires only GLib 2.32, while GLib 2.38 is required to use G_DEFINE_TYPE_WITH_PRIVATE()
.
I don't really understand what's going on, but I think that the behavior of a code using GLIB_VERSION_MAX_ALLOWED
should not change according to the definition of this macro, which is just intended to generate warnings.
In any case, here is a minimal program to show that the offset returned by g_type_add_instance_private()
(from which the deprecation warning comes when GLIB_VERSION_MAX_ALLOWED < 2.38
) is different depending on the definition of GLIB_VERSION_MAX_ALLOWED
, which seems strange to me:
p.c
#include <gtk/gtk.h>
#define MY_APP_TYPE_WINDOW (my_app_window_get_type ())
G_DECLARE_FINAL_TYPE (MyAppWindow, my_app_window, MY_APP, WINDOW, GtkWindow)
struct _MyAppWindow
{
GtkWindow parent;
};
typedef struct _MyAppWindowPrivate
{
gpointer data1;
gpointer data2;
} MyAppWindowPrivate;
G_DEFINE_TYPE_WITH_PRIVATE (MyAppWindow, my_app_window, GTK_TYPE_WINDOW)
static void
my_app_window_class_init (MyAppWindowClass *class)
{
}
static void
my_app_window_init (MyAppWindow *app)
{
}
gint main (gint argc, gchar **argv)
{
MyAppWindow *app;
gtk_init (&argc, &argv);
app = g_object_new (MY_APP_TYPE_WINDOW, NULL);
g_printerr ("%s:data1-offset: %ld\n", G_STRLOC, G_PRIVATE_OFFSET (MyAppWindow, data1));
g_printerr ("%s:data2-offset: %ld\n", G_STRLOC, G_PRIVATE_OFFSET (MyAppWindow, data2));
gtk_widget_destroy (GTK_WIDGET (app));
}
First run with GLIB_VERSION_MAX_ALLOWED
set to 2.38:
$ v=38; gcc p.c -o p $(pkg-config --cflags --libs gtk+-3.0) -DGLIB_VERSION_MIN_REQUIRED=GLIB_VERSION_2_$v -DGLIB_VERSION_MAX_ALLOWED=GLIB_VERSION_2_$v && ./p
p.c:37:data1-offset: -624
p.c:38:data2-offset: -616
Second run with GLIB_VERSION_MAX_ALLOWED
set to 2.36:
$ v=36; gcc p.c -o p $(pkg-config --cflags --libs gtk+-3.0) -DGLIB_VERSION_MIN_REQUIRED=GLIB_VERSION_2_$v -DGLIB_VERSION_MAX_ALLOWED=GLIB_VERSION_2_$v && ./p
p.c: In function 'my_app_window_get_type_once':
p.c:17:1: warning: 'g_type_add_instance_private' is deprecated: Not available before 2.38 [-Wdeprecated-declarations]
17 | G_DEFINE_TYPE_WITH_PRIVATE (MyAppWindow, my_app_window, GTK_TYPE_WINDOW)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/glib-2.0/gobject/gobject.h:26,
from /usr/include/glib-2.0/gobject/gbinding.h:31,
from /usr/include/glib-2.0/glib-object.h:24,
from /usr/include/glib-2.0/gio/gioenums.h:30,
from /usr/include/glib-2.0/gio/giotypes.h:30,
from /usr/include/glib-2.0/gio/gio.h:28,
from /usr/include/gtk-3.0/gdk/gdkapplaunchcontext.h:28,
from /usr/include/gtk-3.0/gdk/gdk.h:32,
from /usr/include/gtk-3.0/gtk/gtk.h:30,
from p.c:1:
/usr/include/glib-2.0/gobject/gtype.h:1359:10: note: declared here
1359 | gint g_type_add_instance_private (GType class_type,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
p.c:37:data1-offset: 16
p.c:38:data2-offset: 24