Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
anna-hope opened this issue Jul 23, 2024 · 1 comment · Fixed by #1607
Closed

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

anna-hope opened this issue Jul 23, 2024 · 1 comment · Fixed by #1607

Comments

@anna-hope
Copy link

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
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 anna-hope@a3ee252, 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.

@bilelmoussaoui
Copy link
Member

The solution is actually pretty easy, doesn't require anything like you did. Just add assert_initialized_main_thread!(); macro call there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants