gtk-rs / gtk4-rs

Rust bindings of GTK 4
https://gtk-rs.org/gtk4-rs/
MIT License
1.9k stars 174 forks source link

Creating custom widget without initializing gtk causes SIGSEGV instead of panic #387

Closed tom-a-wagner closed 3 years ago

tom-a-wagner commented 3 years ago

Instead of panicking like normal widgets, creating a custom widget causes a SIGSEGV on my machine running Fedora 34, gtk4 4.2.1 and gtk4-rs from master.

use gtk4::{glib, subclass::prelude::*};

mod imp {
    use super::*;

    #[derive(Default)]
    pub struct CustomWidget;

    #[glib::object_subclass]
    impl ObjectSubclass for CustomWidget {
        const NAME: &'static str = "CustomWidget";
        type Type = super::CustomWidget;
        type ParentType = gtk4::Widget;
    }

    impl ObjectImpl for CustomWidget {}
    impl WidgetImpl for CustomWidget {}
}

glib::wrapper! {
    pub struct CustomWidget(ObjectSubclass<imp::CustomWidget>)
        @extends gtk4::Widget;
}

impl CustomWidget {
    pub fn new() -> Self {
        glib::Object::new(&[]).unwrap()
    }
}

fn main() {
    CustomWidget::new();
}

Backtrace:

Stack trace of thread 227616:
#0  0x00007fc7834ebb95 gtk_css_value_initial_compute.lto_priv.0 (libgtk-4.so.1 + 0x2f6b95)
#1  0x00007fc783500042 gtk_css_static_style_compute_value (libgtk-4.so.1 + 0x30b042)
#2  0x00007fc7835015f2 gtk_css_static_style_new_compute (libgtk-4.so.1 + 0x30c5f2)
#3  0x00007fc783501741 gtk_css_static_style_get_default (libgtk-4.so.1 + 0x30c741)
#4  0x00007fc7834e970f gtk_css_node_init (libgtk-4.so.1 + 0x2f470f)
#5  0x00007fc7839a409b g_type_create_instance (libgobject-2.0.so.0 + 0x3b09b)
#6  0x00007fc78398bb8d g_object_new_internal (libgobject-2.0.so.0 + 0x22b8d)
#7  0x00007fc78398cb5d g_object_new_with_properties (libgobject-2.0.so.0 + 0x23b5d)
#8  0x00007fc78398d661 g_object_new (libgobject-2.0.so.0 + 0x24661)
#9  0x00007fc7834a7eb2 gtk_widget_init (libgtk-4.so.1 + 0x2b2eb2)
#10 0x00007fc7839a409b g_type_create_instance (libgobject-2.0.so.0 + 0x3b09b)
#11 0x00007fc78398bb8d g_object_new_internal (libgobject-2.0.so.0 + 0x22b8d)
#12 0x00007fc78398cee5 g_object_newv (libgobject-2.0.so.0 + 0x23ee5)
#13 0x0000562581c583c0 n/a (/home/tomwagner/Code/gtk4-sigsegv/target/debug/gtk4-sigsegv + 0x243c0)
bilelmoussaoui commented 3 years ago

You can't just execute the main widget, you need at least an application...

tom-a-wagner commented 3 years ago

You can't just execute the main widget, you need at least an application...

Yes, I minified the example to show the segfault.

bilelmoussaoui commented 3 years ago

Simplified as in removed the gtk::application code or what exactly?

tom-a-wagner commented 3 years ago

Simplified as in removed the gtk::application code or what exactly?

Yes, this is an example taken from a real program that caused a segmentation failure because it created a custom widget before the applications activate callback called.

The point is not that this does not work, because obviously it can't without an application, but that I consider this bug because it would expect the code to cause a panic, not a segmention failure.

sdroege commented 3 years ago

Indeed, we somehow have to add our initialization checks also into the subclass code.

It would probably make sense to put it here https://github.com/gtk-rs/gtk4-rs/blob/8d674b0e61f4f42f7ddf57143d96fe81bfdc4051/gtk4/src/subclass/widget.rs#L578-L582

And the same for all other non-widget types in gtk4.

sdroege commented 3 years ago

This of course also applies to gtk3-rs.