[Freeciv-Dev] possible segfault and help!
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
I've been working on a new dialog for civworld, and have run into this
segfault condition. It is easily caught for what I want to do, and since
the dialog is similar to the create_races_dialog(), in
client/gui-gtk/dialogs.c, it _might_ happen there.
below is a program similar to the dialog (without the cruft) that
reproduces the problem.
The problem: activating the zeroth button does not signal the
"toggled" callback the first time.
The question is why does this happen?
An workaround is to active a non-zero button before creating the signal
(see below and attached patch), but I'd like to know if there's a direct
fix. Anybody?
--mike
/* gcc radio_test.c `gtk-config --libs --cflags` */
#include<gtk/gtk.h>
#include<stdio.h>
struct dlg {
GtkWidget *shell;
GtkWidget **races_toggles;
int i;
};
/*****************************************************************/
static void races_toggles_callback( GtkWidget *w, struct dlg *en)
{
printf("into callback\n");fflush(stdout);/* DEBUG */
en->i = 1; /* valid array index */
}
/*****************************************************************/
static void select_random_race(struct dlg *en)
{
/* setting en->races_toggles[0] -> en->races_toggles[1] cures it */
/* otherwise we get a segfault because of bad array index */
gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(en->races_toggles[0]),
TRUE );
gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(en->races_toggles[en->i]),
TRUE );
printf("%d\n",en->i);fflush(stdout);/* DEBUG */
}
/*****************************************************************/
int main(int argc, char **argv)
{
int i;
char buffer[3];
struct dlg *en;
GSList *group = NULL;
gtk_init(&argc,&argv);
en = (struct dlg *)malloc(sizeof(struct dlg));
en->i = -1; /* invalid array index */
en->shell = gtk_dialog_new();
en->races_toggles = (GtkWidget**)malloc(5*sizeof(GtkWidget*));
for(i=0; i < 5 ; i++) {
sprintf(buffer,"%d",i);
en->races_toggles[i] = gtk_radio_button_new_with_label(group, buffer);
/* setting FALSE -> TRUE cures it */
gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(en->races_toggles[i]),
FALSE);
group =
gtk_radio_button_group(GTK_RADIO_BUTTON(en->races_toggles[i]));
gtk_box_pack_start(GTK_BOX(GTK_DIALOG(en->shell)->vbox),
en->races_toggles[i],FALSE,FALSE,0);
}
for(i=0; i< 5 ; i++)
gtk_signal_connect(GTK_OBJECT(en->races_toggles[i]), "toggled",
races_toggles_callback, (gpointer)en );
select_random_race(en);
gtk_widget_show_all(en->shell);
gtk_main();
return 0;
}
dialogs_patch
Description: Text document
|
|