Clearing a TextView's buffer from a widget inside the TextView sometimes causes a segfault
Steps to reproduce
- create a TextView
- create a TextAnchorChild
- create a Button
- insert Button at TextAnchorChild
- have the Button's call back set the TextView's buffer text to ""
- Segfault (sometimes)
#include <gtk/gtk.h>
static void button_callback(GtkButton* const _button, void* const user_data) {
GtkTextView* const text_view = user_data;
GtkTextBuffer* const buffer = gtk_text_view_get_buffer(text_view);
gtk_text_buffer_set_text(buffer, "", 0);
}
static void show_ui(GtkApplication* const app) {
GtkApplicationWindow* const window = GTK_APPLICATION_WINDOW(gtk_application_window_new(app));
gtk_window_set_title(GTK_WINDOW(window), "bug testing");
GtkButton* const button = GTK_BUTTON(gtk_button_new_with_label("segfault here!"));
GtkTextView* const text_view = GTK_TEXT_VIEW(gtk_text_view_new());
GtkTextBuffer* const buffer = gtk_text_view_get_buffer(text_view);
GtkTextChildAnchor* const anchor = gtk_text_child_anchor_new();
GtkTextIter iter;
gtk_text_buffer_get_end_iter(buffer, &iter);
gtk_text_buffer_insert_child_anchor(buffer, &iter, anchor);
gtk_text_view_add_child_at_anchor(text_view, GTK_WIDGET(button), anchor);
g_signal_connect(button, "clicked", G_CALLBACK(button_callback), g_object_ref(text_view));
gtk_window_set_child(GTK_WINDOW(window), GTK_WIDGET(text_view));
gtk_widget_show(GTK_WIDGET(window));
}
int main(int const argc, char** const argv) {
GtkApplication* const app = gtk_application_new("com.example.demo", G_APPLICATION_DEFAULT_FLAGS);
g_signal_connect(app, "activate", G_CALLBACK(show_ui), NULL);
g_application_run(G_APPLICATION(app), argc, argv);
return 0;
}
Current behavior
The program will sometimes segfault
Expected outcome
The TextView should clear its contents including the button without segfaulting
Version information
GTK Version: 4.8.3-1
OS: Fedora 37