I am trying to create a GUI program using Glade and GTK+ 3 to write set and write configuration data to a custom microchip. I am trying to implement a feature to save/load previous configurations into my GUI. After saving to a file and loading, some of my callback functions now cause a segfault. One example being a callback for a "global enable" checkbox.
I am currently trying to read data from my file and using that data to update the state of the GUI (as well as global variables used for configuring the IC). I update my checkbox as follows:
/* Global widget handles */
GtkWidget* GEN_CB_h;
GtkWidget* Neg_Pol_CB_h;
//more widgets here
/* Global configuration variables */
char gen;
char neg_pol;
//more variables here
/* Load data from a stored configuration file */
void on_Load_File_Button_selection_changed()
{
GtkFileChooser* file = GTK_FILE_CHOOSER(Load_File_Box_h);
gchar* filename = gtk_file_chooser_get_filename(file);
FILE* fd = fopen((const char*)filename, "r");
if(!fd)
{
perror("Failed to open file\n");
exit(EXIT_FAILURE);
}
fread(&gen, sizeof(gen), 1, fd);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(GEN_CB_h), gen);
fread(&neg_pol, sizeof(neg_pol), 1, fd);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(neg_pol_cb), neg_pol);
/* read more data and update more GUI elements here */
}
/* Callback for negative polarity bit checkbox */
void on_Neg_Pol_CB_toggled()
{
GtkToggleButton* neg_pol_cb = GTK_TOGGLE_BUTTON(Neg_Pol_CB_h);
neg_pol = (char)gtk_toggle_button_get_active(neg_pol_cb);
printf("Neg pol toggled: %s\n", (neg_pol) ? "ON":"OFF");
}
/* Callback for global enable bit checkbox */
void on_GEN_CB_toggled()
{
printf("gen_cb = %p\n", GEN_CB_h); //print before cast for debugging
GtkToggleButton* gen_cb = GTK_TOGGLE_BUTTON(GEN_CB_h);
gen = (char)gtk_toggle_button_get_active(gen_cb);
printf("GEN toggled: %s\n", (gen) ? "ON":"OFF");
}
The issue I am having is that after loading a new configuration (i.e. triggering my Load_File_Button callback) my GEN_CB callback triggers a segfault on this line:
GtkToggleButton* gen_cb = GTK_TOGGLE_BUTTON(GEN_CB_h);
I used GDB to see what the cause of the segfault was and it reported it as:
Thread 1 "cfd_gui" received signal SIGSEGV, Segmentation fault.
0x00007ffff7395ea0 in g_type_check_instance_cast () from /usr/lib/libgobject-2.0.so.0
I also included a printf statement to check and see if anything was happening to the pointer between loads and found that the pointer value is being modified after this line is executed:
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(GEN_CB_h), gen);
I am still new to GTK3 so I am probably using something wrong but have not been able to figure it out. Here are the outputs of my printf debug statements:
gen_cb = 0x55cd1aa04240
GEN toggled: OFF
Loaded configuration from file: /home/borabutt/github/CFD-RPI-Test/src/C/gui/test.dat
gen_cb = 0x55ea7bb68240
The first line shows the pointer to GEN_CB_h before configuration data is loaded. Then as can clearly be seen, the pointer value is changed after the configuration data is loaded. My investigations have shown this change occurs after setting the state of the checkbox as shown above.
I need to be able to update the state of many checkboxes and combo boxes to properly reflect the loaded state from the file. Right now the GUI works completely fine until I load data from a file. Any help and insight is appreciated.
Aucun commentaire:
Enregistrer un commentaire