Re: Passing Struct to g_signal_connect




Thank you. However, now I'm getting a segfault when trying to access any of
the elements of the passed struct. Does it matter that the button to which
the signal is attached is actually in the struct I'm passing to the
callback? This is what GDB gives me

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xb72fa940 (LWP 14464)]
0x08049f16 in on_button2_clicked (button=0x807f418, by_ptr=0x0)
    at callbacks.h:56
56              printf("Qs: %i\n", by_ptr->globals.Qs);

Thanks for your help


Till Harbaum / Lists wrote:

Hi,

you have to make sure that the parameters of your callback function
exactly match what's been described for this particular event. In your
case your have to check the "clicked" event for a GtkButton which gives
us:

void user_function(GtkButton *button, gpointer   user_data)  

So this is how your callback function should look like:
void on_button2_clicked(GtkButton *button, struct allStructs *by_ptr)

Your struct should be the _second_ parameter and not the first (any only)
one your made it. What you did is: You got a pointer to the GtkButton
structure 
of your button and used _that_ as a pointer to your own struct. You
are likely messing up the Button structure ...

This is one ugly thing with the C bindings and the fact that everything
is just casted by this G_CALLBACK() macro. That way the compiler isn't
able to check these things and errors like yours happen. And these can
be very nasty bugs since they cause people to write to memory they
are not supposed to write to. A classic door for hackers ...

Till

Am Sonntag 19 Oktober 2008 schrieb beginner.c:

I need to pass a struct using g_signal_connect, but the issue I'm having
is
that I can't alter the elements of the struct within the called function
e.g.

void on_button2_clicked (struct allStructs *by_ptr)

{
     gtk_label_set_text ((GtkLabel*)by_ptr->widgets.label, "whatever");
}

main
{
blah blah
struct allStructs baseStruct;

g_signal_connect (G_OBJECT(baseStruct.widgets.nextButton), "clicked",
G_CALLBACK (on_button2_clicked), &baseStruct);
}

Any idea's? If I pass this struct to any other function I have no problem
performing functions like in the callback. For some reason, I can't in
the
callback.



_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list



-- 
View this message in context: 
http://www.nabble.com/Passing-Struct-to-g_signal_connect-tp20055372p20062941.html
Sent from the Gtk+ - Apps Dev mailing list archive at Nabble.com.




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