Strange segfault, can anyone help me?



I have a problem and i would appreciate some help.

In my program when the user saves a file and the file already exists, then a 
dialog pops up and asks the user if they want to overwrite it.
To do this i use a technique that Owen showed in a mail earlier, i do 
recursive calls to gtk_main(). This works great normally, but if i select a 
file by double-clicking on it i get a segfault.

Here is the code in question, it's a callback from "clicked" on the ok button 
of a fileselection:

static void
file_save_as_dialog_ok_callback (GtkWidget        *w,
				 GtkFileSelection *fs)
{
  char *filename;
  Diagram *dia;
  struct stat stat_struct;
  
  dia = gtk_object_get_user_data(GTK_OBJECT(fs));
  
  filename = gtk_file_selection_get_filename (GTK_FILE_SELECTION (fs));

  if (stat(filename, &stat_struct)==0) {
    GtkWidget *dialog = NULL;
    GtkWidget *button, *label;
    char buffer[300];
    int result;

    dialog = gtk_dialog_new();
  
    gtk_signal_connect (GTK_OBJECT (dialog), "destroy", 
			GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
    
    gtk_window_set_title (GTK_WINDOW (dialog), "File already exists");
    gtk_container_border_width (GTK_CONTAINER (dialog), 0);
    snprintf(buffer, 300,
	     "The file '%s' already exists.\n"
	     "Do you want to overwrite it?", filename);
    label = gtk_label_new (buffer);
  
    gtk_misc_set_padding (GTK_MISC (label), 10, 10);
    gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), 
			label, TRUE, TRUE, 0);
  
    gtk_widget_show (label);

    result = FALSE;
    
    button = gtk_button_new_with_label ("Yes");
    GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
    gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area), 
			button, TRUE, TRUE, 0);
    gtk_widget_grab_default (button);
    gtk_signal_connect (GTK_OBJECT (button), "clicked",
			GTK_SIGNAL_FUNC(set_true_callback),
			&result);
    gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
			       GTK_SIGNAL_FUNC (gtk_widget_destroy),
			       GTK_OBJECT (dialog));
    gtk_widget_show (button);
    
    button = gtk_button_new_with_label ("No");
    GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
    gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
			button, TRUE, TRUE, 0);
    
    gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
			       GTK_SIGNAL_FUNC (gtk_widget_destroy),
			       GTK_OBJECT (dialog));
    
    gtk_widget_show (button);

    gtk_widget_show (dialog);

    /* Make dialog modal: */
    gtk_widget_grab_focus(dialog);
    gtk_grab_add(dialog);

    gtk_main();

    if (result==FALSE) {
      gtk_grab_remove(GTK_WIDGET(fs));
      gtk_widget_destroy(GTK_WIDGET(fs));
      return;
    }
  }

  diagram_set_filename(dia, filename);
  diagram_save(dia, filename);
  
  gtk_grab_remove(GTK_WIDGET(fs));
  gtk_widget_destroy(GTK_WIDGET(fs));
}

If i just enter a name or select some name and then click on ok everything 
works, but if i double-click on a filename i get a segfault after having 
answered yes or no on the 'are you sure' dialig.

Here is the trace:
Program received signal SIGSEGV, Segmentation fault.
gtk_signal_real_emit (object=0x809c330, signal=0x807c8cc, params=0xbffff2d0) 
at gtksignal.c:1404
1404      if (GTK_RUN_TYPE (signal->run_type) != GTK_RUN_FIRST  && signal->
function_offset != 0)
(gdb) bt
#0  gtk_signal_real_emit (object=0x809c330, signal=0x807c8cc, 
params=0xbffff2d0) at gtksignal.c:1404
#1  0x4006e13b in gtk_signal_emit (object=0x809c330, signal_id=73) at 
gtksignal.c:510
#2  0x4002f18c in toggle_row (clist=0x809c330, row=33, column=0, 
event=0x80b1e68) at gtkclist.c:3004
#3  0x4002d394 in gtk_clist_button_press (widget=0x809c330, event=0x80b1e68) 
at gtkclist.c:2057
#4  0x40094769 in gtk_widget_marshal_signal_4 (object=0x809c330, 
func=0x4002d290 <gtk_clist_button_press>, func_data=0x0, args=0xbffff658) at 
gtkwidget.c:3415
#5  0x4006faa9 in gtk_signal_real_emit (object=0x809c330, signal=0x807c134, 
params=0xbffff658) at gtksignal.c:1409
#6  0x4006e13b in gtk_signal_emit (object=0x809c330, signal_id=19) at 
gtksignal.c:510
#7  0x4009213c in gtk_widget_event (widget=0x809c330, event=0x80b1e68) at 
gtkwidget.c:2037
#8  0x400503d5 in gtk_propagate_event (widget=0x809c330, event=0x80b1e68) at 
gtkmain.c:1676
#9  0x4004ef96 in gtk_main_iteration_do (blocking=1 '\001') at gtkmain.c:712
#10 0x4004eccb in gtk_main_iteration () at gtkmain.c:506
#11 0x4004eb35 in gtk_main () at gtkmain.c:412
#12 0x80568b8 in main (argc=1, argv=0xbffff994) at main.c:30

My newbie guess is that the fs dialog is destroyed when there are outstanding 
callbacks for it, but i don't understand why or how i can fix it.

/ Alex





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]