Strange Callback Behavior



Dear GTK'lers,

I think I must have misunderstood something about the use of callback functions in GTK, but despite some 
amount of testing, I cannot get to the bottom of it. 

My goal is to have a label on the GUI be changed after a user chooses a file. To do this, I define a struct



struct menu_context {
        GtkWidget *datalabel;
        GtkWidget *genelabel;
        GtkWidget *samplelabel;
};



ach of these is initialized:
        mcontext = g_malloc(sizeof (struct menu_context) );     
        mcontext->datalabel = gtk_label_new( "Open file ..." ); 
        (...)

and the label is placed in the GUI. So far, so good.

This struct is passed to the menu constructor (the code is much the same as in the online tutorial):

        menubar = get_menubar_menu (mainwindow,mcontext);       

Then, there is a function that gets called when "File/Open Data Matrix" is clicked on from the menu. This 
makes use of another context struct to pass the pointer to the file-section widget and the label that needs 
to be updated:


struct openfile_context{
        GtkWidget *fs;
        GtkWidget *label;
};


void open_data_file(gpointer callback_data, guint callback_action, GtkWidget  *widget) {
  GtkWidget *filew;
        struct openfile_context * of_context;
        struct menu_context *m_context;
        const char *tmp;
        of_context = g_malloc(sizeof(struct openfile_context));
        m_context = (struct menu_context *)callback_data;

        filew = gtk_file_selection_new ("Open Data Matrix File");

        of_context->fs = filew;
        of_context->label = m_context->datalabel;

        
        g_print("In opendatafile, ptr (gpointer)of_context is %p \n", (gpointer)of_context);
        g_print("In opendatafile, ptr (gpointer)filew is %p \n", (gpointer)filew);
        g_print("In opendatafile, ptr (gpointer)of_context->fs is %p \n", (gpointer)of_context->fs);
        g_print("In opendatafile, ptr (gpointer)of_context->label is %p \n", (gpointer)of_context->label);

         g_signal_connect (G_OBJECT (GTK_FILE_SELECTION (filew)->ok_button),
                                                                                "clicked", 
                                                                                G_CALLBACK 
(data_file_ok_sel), 
                                                                                 of_context);

(...)

In the function data_file_ok_sel, however, for some reason the pointers 
context->fs and context->label have different values, and the program dumps core when it tries to read 
constext->fs as a file selection widget:


        context = (struct openfile_context *) ptr;

        g_print("In CALLBACK pointer for callback  is %p\n",context); 
        g_print("In CALLBACK pointer for callback->fs  is %p\n",context->fs); 
        g_print("In CALLBACK pointer for callback->label  is %p\n",context->label); 
        g_print("In CALLBACK pointer is %p\n",GTK_FILE_SELECTION (context->fs)); 


Here is the output from the shell:


In opendatafile, ptr (gpointer)of_context is 0x810caf8
In opendatafile, ptr (gpointer)filew is 0x813c010
In opendatafile, ptr (gpointer)of_context->fs is 0x813c010
In opendatafile, ptr (gpointer)of_context->label is 0x8094828

In CALLBACK pointer for callback  is 0x810caf8
In CALLBACK pointer for callback->fs  is 0x636e6143
In CALLBACK pointer for callback->label  is 0xb7006c65
Segmentation fault (core dumped)


What is happening here? What is the correct way of passing the user data for such a callback?

Thanks to all,

Peter





 



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