Linking GTK3 (On Linux) in a WebUI Zig project causes instant segfault on program startup.
OS: I tested it both on Fedora 43 and Arch Linux Zig version 0.14.1
I wanted to add OS file picker dialogs to my project and to do that, I tried using nfd-zig and osdialog-zig.
However, when importing any of these libraries, I get instant segfault on program startup, like this:
/usr/bin/zig build run
Segmentation fault at address 0x7f1182249b28
???:?:?: 0x7f1182249b28 in ??? (???)
Unwind error at address `libwebui.so:0x7f1182249b28` (error.AddressOutOfRange), trace may be incomplete
???:?:?: 0x7f1181789aa6 in ??? (libgtk-3.so.0)
???:?:?: 0x7f1181efddaf in ??? (libglib-2.0.so.0)
???:?:?: 0x7f1181785bf8 in ??? (libgtk-3.so.0)
???:?:?: 0x7f1181789821 in ??? (libgtk-3.so.0)
/home/wilson2/.cache/zig/p/nfdzig-0.1.0-11fxvN6IBgD5rvvfjrw1wPqibMsbUJ-h2ZcGR6FOEvrm/nativefiledialog/src/nfd_gtk.c:175:11: 0x1037ff2 in NFD_OpenDialog (/home/wilson2/.cache/zig/p/nfdzig-0.1.0-11fxvN6IBgD5rvvfjrw1wPqibMsbUJ-h2ZcGR6FOEvrm/nativefiledialog/src/nfd_gtk.c)
if ( !gtk_init_check( NULL, NULL ) )
^
/home/wilson2/.cache/zig/p/nfdzig-0.1.0-11fxvN6IBgD5rvvfjrw1wPqibMsbUJ-h2ZcGR6FOEvrm/src/lib.zig:25:36: 0x10e6f99 in openFileDialog (webui_gtk_crash_demo)
const result = c.NFD_OpenDialog(if (filter != null) filter.? else null, if (default_path != null) default_path.? else null, &out_path);
^
/home/wilson2/Documents/Coding/Zig/webui-gtk-crash-demo/src/main.zig:6:27: 0x10e6e38 in main (webui_gtk_crash_demo)
if (nfd.openFileDialog(null, null)) |path_opt|
^
/usr/lib/zig/std/start.zig:660:37: 0x10e7812 in main (webui_gtk_crash_demo)
const result = root.main() catch |err| {
^
???:?:?: 0x7f118140f5b4 in ??? (libc.so.6)
???:?:?: 0x7f118140f667 in ??? (libc.so.6)
???:?:?: 0x1037114 in ??? (???)
run
└─ run webui_gtk_crash_demo failure
I created a repo with minimal reproduction examples, so that you can check it:
https://github.com/jarcopolo/webui-gtk-crash-demo
Apart from that, thanks for your work on the project, on Windows everything works alright.
I will try to reproduce this problem I will address this issue later this week.
I have a feeling this is not a WebUI bug.
Issue:
-
gtk_init_check()is failed maybe becausewebui_gtk_crash_demoattempts to use theosdialog_filefunction before GTK initialization - Check out this nfd-zig line
Info:
WebUI internal GTK initialization is _webui_load_gtk_and_webkit(), gets called in webui_set_center() or webui_show_wv().
Solution 1:
Load and make WebUI initialization GTK first before anything else. Make sure your project is running like this.
- Import WebUI
- (Main Thread) Create a window
- (Main Thread) Call
webui_set_center()orwebui_show_wv()to initialize GTK - Now you are safe to call any nfd-zig and osdialog-zig functions because GTK is initialized by WebUI
Solution 2:
Make your own GTK project, add nfd-zig and osdialog-zig for files picker, and add WebUI only as web server and JavaScript bridge
Your system has gtk2 and gtk3
your demo works ok in windows, I'm trying build it under wsl archlinux
https://manus.im/share/file/923f689f-ffd2-48ed-913f-57028cab6127
You can view this document, and on my archlinux wsl, it fails to build
The log reports the library osdialog-zig has strange problem
Thank you for your testing and insights. Regarding the osdialog.a error, I also got the same warning messages, but this doesn't appear to be the problem stopping my example from running.
I attached backtraces of my faulty programs to the bottom of this post.
In the meantime, I tested on more of my devices and also made a functionally identical project in C using the same libraries.
The C version also reported segfault on startup (I was using 2.5.0-beta.3 version release from 7th of March)
After I updated the Zig dependency to use latest commit (I was probably using the version from 24th of August), it is working now.
Later, I built the WebUI library from source using the latest commit, put it into my C test project and it also began to work.
I can provide more information if you are interested.
Thank you for your work.
Backtrace: Zig program, Only WebUI invocation, GTK3 linked
``` Thread 1 "webui_gtk_crash" received signal SIGSEGV, Segmentation fault. 0x00000000011da190 in gdk_display_get_default () (gdb) bt #0 0x00000000011da190 in gdk_display_get_default () #1 0x00007ffff77a5529 in _gtk_modules_init.constprop.0 (gtk_modules_args=gtk_modules_args@entry=0x0, argv=0x0, argc=0x0) at ../gtk/gtkmodules.c:525 #2 0x00007ffff7789aa7 in do_post_parse_initialization (argc=0x0, argv=0x0) at ../gtk/gtkmain.c:766 #3 post_parse_hook (context=Backtrace: Zig program, NFD + WebUI invocation, GTK3 linked
``` Thread 1 "webui_gtk_crash" received signal SIGSEGV, Segmentation fault. 0x00007ffff7449b28 in gdk_display_get_default () from /home/wilson2/Documents/Coding/Zig/webui-gtk-crash-demo/.zig-cache/o/86c5389185ec1e9caacf99aedadd0e51/libwebui.so (gdb) bt #0 0x00007ffff7449b28 in gdk_display_get_default () at /home/wilson2/Documents/Coding/Zig/webui-gtk-crash-demo/.zig-cache/o/86c5389185ec1e9caacf99aedadd0e51/libwebui.so #1 0x00007ffff77a5529 in _gtk_modules_init.constprop.0 (gtk_modules_args=gtk_modules_args@entry=0x0, argv=0x0, argc=0x0) at ../gtk/gtkmodules.c:525 #2 0x00007ffff7789aa7 in do_post_parse_initialization (argc=0x0, argv=0x0) at ../gtk/gtkmain.c:766 #3 post_parse_hook (context=Backtrace: C program, NFD + WebUI invocation, GTK3 linked
``` Thread 1 "pickerc" received signal SIGSEGV, Segmentation fault. 0x000000000044e2a8 in g_signal_connect_data () (gdb) bt #0 0x000000000044e2a8 in g_signal_connect_data () #1 0x00007ffff79a5556 in _gtk_modules_init.constprop.0 (gtk_modules_args=gtk_modules_args@entry=0x0, argv=0x0, argc=0x0) at ../gtk/gtkmodules.c:526 #2 0x00007ffff7989aa7 in do_post_parse_initialization (argc=0x0, argv=0x0) at ../gtk/gtkmain.c:766 #3 post_parse_hook (context=
Thank you for the test, we will check it soon. Yes, we are interested in this to know if it's a bug in WebUI or in your project.
interesting, I'm not good at gtk, maybe you @AlbertShown can resolve this?
https://github.com/jarcopolo/webui_with_gtk_filepicker Created this C (with Cmake) repo to test various WebUI versions. Looks like the issue is already solved since 17 of October, all commits newer than this one don't cause GTK problems anymore: https://github.com/webui-dev/webui/commit/bbe25a58b25e2617b19db85d977189710e7769fe