问题
I am writing a program using GTK in C. I am using GtkListStore to display some data coming from a database. After a particular signal I want to remove all the rows in the GtkListStore. I used gtk_list_store_clear() function, but it raises Segmentation Fault.
What is wrong with my code?
//Globally declared
GtkListStore *liststore2;
//Inside main() function
liststore2 = GTK_LIST_STORE(gtk_builder_get_object(builder, "liststore2"));
//Inside signal handler function
gtk_list_store_clear(liststore2); //Error comes from here
回答1:
If you destroy the builder object (using g_object_unref(builder)
) before the signal handler runs, liststore2 may point to freed memory.
That happens if liststore2 is free-standing (i.e., not ref'ed by something else, for example a GtkTreeView)
gtk_builder_get_object does not increment the reference count on object
来源:https://stackoverflow.com/questions/58961074/gtk-list-store-clear-function-raises-segmentation-fault-in-c