极其基本的 Glade 配置段错误

Extremely Basic Glade Configuration Segfaults

我正在尝试使用 GTK3+ 和 Glade 进行设置。不幸的是,我可以在网上找到的最基本的设置是 sefaulting。在 Glade 中,我刚刚创建了一个 ID 为 window_main 的基本 window。我不确定这是怎么回事。

bytebowl.c

#include <gtk/gtk.h>

int main(int argc, char* argv[]) 
{

    GtkBuilder *builder;
    GtkWidget *window, *label;

    builder = gtk_builder_new();
    gtk_builder_add_from_file(builder, "glade/hello.glade", NULL);

    window = GTK_WIDGET(gtk_builder_get_object(builder, "window_main"));

    g_object_unref(builder);
    gtk_widget_show(window);
    gtk_main();

    return 0;
}

hello.glade

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
  <requires lib="gtk+" version="3.24"/>
  <object class="GtkWindow" id="window_main">
    <property name="can-focus">False</property>
    <child>
      <placeholder/>
    </child>
  </object>
</interface>
[michael@thinkpad ~/Code/ByteBowl]$ ./bytebowl

(process:8290): Gtk-CRITICAL **: 15:01:02.191: _gtk_style_provider_private_get_settings: assertion 'GTK_IS_STYLE_PROVIDER_PRIVATE (provider)' failed

(process:8290): Gtk-CRITICAL **: 15:01:02.195: _gtk_style_provider_private_get_settings: assertion 'GTK_IS_STYLE_PROVIDER_PRIVATE (provider)' failed

(process:8290): Gtk-CRITICAL **: 15:01:02.195: _gtk_style_provider_private_get_settings: assertion 'GTK_IS_STYLE_PROVIDER_PRIVATE (provider)' failed
zsh: segmentation fault (core dumped)  ./bytebowl

Makefile...

CC=clang

CFLAGS=`pkg-config --cflags gtk+-3.0`
LFLAGS=`pkg-config --libs gtk+-3.0`

all: bytebowl

bytebowl:
    $(CC) $(CFLAGS) -o bytebowl src/bytebowl.c $(LFLAGS)

这是 dmesg 输出...

[michael@thinkpad ~/Code/ByteBowl]$ sudo dmesg
[...snip...]
[16452.137217] bytebowl[8256]: segfault at 18 ip 00007f18a6591e8c sp 00007fff910ef2e0 error 4 in libgtk-3.so.0.2404.25[7f18a6506000+382000]
[16452.137249] Code: ff 0f 1f 44 00 00 48 83 c4 08 5b 5d c3 90 48 8b 7f 10 48 85 ff 74 07 e9 42 ed ff ff 66 90 48 83 ec 08 48 89 d7 e8 14 31 18 00 <48> 8b 40 18 48 8b 78 10 67 e8 06 ac 09 00 48 83 c4 08 48 89 c7 e9
[16452.137393] audit: type=1701 audit(1623265257.250:356): auid=1000 uid=1000 gid=1000 ses=1 pid=8256 comm="bytebowl" exe="/home/michael/Code/ByteBowl/bytebowl" sig=11 res=1

您需要在 Gtk 的任何其他函数之前调用 gtk_init()

此外,gtk_builder_get_object() 不会将小部件的所有权传递给调用者,因此在您这样做的构建器上调用 g_object_unref() 可能不是一个好主意。