Re: gpointer and callback



On Thu, 30 Oct 2003 10:22:47 -0300 Ariel Fritz wrote:

I´m trying to pass a string to a callback but I don´t know how.

I´ve tryed this:

char str[256];
strcpy(str,"hello,world");
g_signal_connect (G_OBJECT (clist), "select_row", G_CALLBACK (MyCallback), str );


void Call_Menu(GtkWidget *widget, gint row, gint column, GdkEventButton *event, gpointer data){

    g_printf("%s\n", (char *) data);

{

This is problematic, because the char array "str" is allocated on the
stack of the function in which you define it.  After you return from
that function, the stack is "freed", and the pointer to str points to an
undefined region on the stack.  You should allocate a string (char *)
with malloc (or copy it with strndup), or, even better, use the
g_string_* functions.


regards,
michael



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