I need some help. I have tried multiple strategies, but I still get the same segmentation fault error in my application, and I have no idea why I’m getting this segmentation fault error. Maybe it is an internal bug in GTK, but I wanted to check here first.
I have a very small C++ project, using the GTK C API (I prefer the C API over the C++ API), built with CMake. My src/main.cpp
is:
#include <iostream>
#include "gtk/gtk.h"
int main(int argc, char* argv[]) {
std::cout << "[INFO]: Starting application ...\n";
GError* error = NULL;
GtkBuilder* gtk_builder = gtk_builder_new();
std::string ui_resource_path = "/org/caju_gtk_gui.ui";
if (gtk_builder_add_from_resource(gtk_builder, ui_resource_path.c_str(), &error)) {
std::cout << "[INFO]: GTK Resource bundle loaded succesfully.\n";
} else {
std::cerr << "[ERROR]: Failed to add GTK Resource bundle: " << error->message << ".\n";
}
return 1;
}
You can see at main.cpp
that I’m using a Gio resource, to load my UI XML description. My resources XML description is at assets/caju.gresource.xml
, and it contains the following:
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/org">
<file compressed="true" preprocess="xml-stripblanks">caju_gtk_gui.ui</file>
</gresource>
<gresource prefix="/org/styles">
<file>style.css</file>
</gresource>
<gresource prefix="/org/icons">
<file>icons/caju-icon.svg</file>
</gresource>
<gresource prefix="/org/fonts">
<file>fonts/Lato/Lato-Black.ttf</file>
<file>fonts/Lato/Lato-Bold.ttf</file>
<file>fonts/Lato/Lato-Italic.ttf</file>
<file>fonts/Lato/Lato-Regular.ttf</file>
<file>fonts/Work-sans/WorkSans-Black.ttf</file>
<file>fonts/Work-sans/WorkSans-BlackItalic.ttf</file>
<file>fonts/Work-sans/WorkSans-Medium.ttf</file>
<file>fonts/Work-sans/WorkSans-Bold.ttf</file>
<file>fonts/Work-sans/WorkSans-Italic.ttf</file>
<file>fonts/Work-sans/WorkSans-Regular.ttf</file>
<file>fonts/Inter/Inter_18pt-LightItalic.ttf</file>
<file>fonts/Inter/Inter_18pt-Light.ttf</file>
</gresource>
</gresources>
My XML UI description caju_gtk_gui.ui
contains the following:
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk" version="4.0" />
<object class="GtkApplicationWindow" id="CajuGtkAppWindow">
<property name="title">Caju Application</property>
<property name="default-height">500</property>
<property name="default-width">720</property>
<child>
<object class="GtkBox">
<property name="orientation">vertical</property>
<property name="valign">center</property>
<property name="vexpand">1</property>
<property name="spacing">6</property>
<property name="margin-start">12</property>
<property name="margin-end">12</property>
<property name="margin-top">12</property>
<property name="margin-bottom">12</property>
<child>
<object class="GtkLabel" id="welcome_text">
<property name="label">Welcome to Caju! Click on "Browse" to start exploring Raw images.</property>
<style> <class name="welcomeLabel"/> </style>
</object>
</child>
<child>
<object class="GtkButton" id="welcome_button">
<property name="label">Browse</property>
<property name="margin-top">12</property>
<property name="margin-bottom">12</property>
<property name="margin-start">12</property>
<property name="margin-end">12</property>
<property name="can-shrink">TRUE</property>
<style> <class name="welcomeButton"/> </style>
</object>
</child>
</object>
</child>
</object>
</interface>
When I compile and run this program, I get a segmentation fault error:
[INFO]: Starting application ...
Segmentation fault (core dumped)
To investigate this error, I started GDB, and executed the program to see where exactly the segmentation fault error was being generated. As you can see below, the segmentation fault is being generated inside a GTK private function that is responsible for generating the default CSS Provider for the application.
(gdb) r
[INFO]: Starting application ...
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff76e24c5 in gtk_style_provider_get_settings (provider=0x0)
at ../../../gtk/gtkstyleprovider.c:137
warning: 137 ../../../gtk/gtkstyleprovider.c: No such file or directory
I also asked GDB to print the complete backtrace to see how the program got to this specific point, and it is like this:
(gdb) bt
#0 0x00007ffff76e24c5 in gtk_style_provider_get_settings (provider=0x0)
at ../../../gtk/gtkstyleprovider.c:137
#1 gtk_css_value_initial_compute
(value=<optimized out>, property_id=1, provider=0x0, style=0x555555796940 [GtkCssStaticStyle], parent_style=0x0) at ../../../gtk/gtkcssinitialvalue.c:52
#2 0x00007ffff76f5673 in _gtk_css_initial_value_new_compute
(parent_style=<optimized out>, style=0x555555796940 [GtkCssStaticStyle], provider=0x0, property_id=1) at ../../../gtk/gtkcssinitialvalue.c:134
#3 gtk_css_static_style_compute_value
(style=style@entry=0x555555796940 [GtkCssStaticStyle], provider=provider@entry=0x0, parent_style=parent_style@entry=0x0, id=1, specified=0x0, section=0x0)
at ../../../gtk/gtkcssstaticstyle.c:1097
#4 0x00007ffff76f6819 in gtk_css_core_values_new_compute
(lookup=0x7fffffffba60, parent_style=0x0, provider=0x0, sstyle=0x555555796940 [GtkCssStaticStyle]) at ../../../gtk/gtkcssstaticstyle.c:245
#5 gtk_css_lookup_resolve
(parent_style=<optimized out>, sstyle=0x555555796940 [GtkCssStaticStyle], provider=0x0, lookup=0x7fffffffba60) at ../../../gtk/gtkcssstaticstyle.c:934
#6 gtk_css_static_style_new_compute
(provider=provider@entry=0x0, filter=filter@entry=0x7fffffffc070, node=node@entry=0x0, change=<optimized out>, change@entry=0)
at ../../../gtk/gtkcssstaticstyle.c:1026
#7 0x00007ffff76f6961 in gtk_css_static_style_get_default ()
at ../../../gtk/gtkcssstaticstyle.c:711
#8 0x00007ffff76df607 in gtk_css_node_init
(cssnode=0x55555578e4f0 [GtkCssNode]) at ../../../gtk/gtkcssnode.c:616
#9 0x00007ffff70c4fbb in g_type_create_instance (type=<optimized out>)
at ../../../gobject/gtype.c:1945
#10 0x00007ffff70aaa64 in g_object_new_internal
(class=0x55555578e3f0, params=0x0, n_params=0)
at ../../../gobject/gobject.c:2603
#11 0x00007ffff70ac016 in g_object_new_internal
(n_params=<optimized out>, params=<optimized out>, class=<optimized out>)
at ../../../gobject/gobject.c:2600
#12 g_object_new_with_properties
(object_type=<optimized out>, n_properties=<optimized out>, names=names@entry=0x0, values=values@entry=0x0) at ../../../gobject/gobject.c:2766
#13 0x00007ffff70acf71 in g_object_new
(object_type=<optimized out>, first_property_name=first_property_name@entry=0x0) at ../../../gobject/gobject.c:2412
#14 0x00007ffff7688ef4 in gtk_css_widget_node_new
(widget=0x55555578db80 [GtkWidget]) at ../../../gtk/gtkcsswidgetnode.c:160
#15 gtk_widget_init
(instance=0x55555578db80 [GtkWidget], g_class=0x55555578c3a0)
at ../../../gtk/gtkwidget.c:2368
#16 0x00007ffff70c4fbb in g_type_create_instance (type=<optimized out>)
Can someone help me understand why this error is happening?