gtk-rs / gir

Tool to generate rust bindings and user API for glib-based libraries
https://gtk-rs.org/gir/book/
MIT License
230 stars 102 forks source link

gtk-4 generated builders should panic if GTK hasn't been initialized #1590

Open anna-hope opened 1 month ago

anna-hope commented 1 month ago

Currently, trying to use some builders before initializing GTK can cause a segfault. Instead, the generated builder should check if GTK has been initialized, and panic if not.

Minimal example ```rust use gtk4::prelude::*; use gtk4::{Application, ApplicationWindow, glib}; const APP_ID: &str = "me.annahope.gtk4-rs-segfault"; fn build_ui(app: &Application) { let window = ApplicationWindow::builder() .application(app) .title("Segfault") .build(); // Segfaults if GTK hasn't been initialized. window.present(); } fn main() -> glib::ExitCode { let app = Application::builder().application_id(APP_ID).build(); // app.connect_activate(build_ui); build_ui(&app); // Segfaults. app.run() } ```

I tried fixing this naively by adding a check to the builder codegen code https://github.com/anna-hope/gir/commit/a3ee2522917a49578fb654cf7a9523d5396ae9a3, but then realized that we definitely can't do this for all builders because:

I briefly investigated the builder_properties to see if we could set some field there, but couldn't find an easy way to add one. I could look into it further, but I am brand new to this project and GTK overall, so perhaps there is a better way to accomplish this that I don't know about.