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

RE: gpointer and callback



> > 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.

Thats right, try:

	g_signal_connect (G_OBJECT (cliet), 
		"select_row", 
		G_CALLBACK (MyCallback), 
		g_strdup_printf("hello_world"));


or

	char str[256];
	strcpy(str, "hello,world");

	g_signal_connect (G_OBJECT (cliet), 
		"select_row", 
		G_CALLBACK (MyCallback), 
		g_strdup(str));



	void Call_Menu(GtkWidget *widget, gint row, gint column, GdkEventButton *event, gchar *str)
	{
		if(!str) return;

		g_message("str is '%s'", str);
		g_free(str);
	}


Regards,
Martyn



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