RE: signal_connect ??



Title: RE: signal_connect ??

I forgot to say that is not possible to send a temporal reference (like a const char *) as user data parameter to a callback. This is because the callback function will be called later when this reference no longer exists. You need to provide a global variable reference or a pointer created on the heap, not on the stack:


void my_init_function(void)
{
   gchar buf[256] = "My string";

   ....

   /*
    * This is wrong since buf is a temporal variable stored in
    * the stack. When this function exits this reference will be
    * no longer valid, by the time the event is produced and the
    * callback function called again.
    */
   gtk_signal_connect (GTK_OBJECT (main_window),
                       "delete_event",
                       GTK_SIGNAL_FUNC (file_quit_cmd_callback),
                       &buf);
   ...
}

void my_init_function(void)
{
   gchar *buf;

   buf = g_strdup ("My string");

   /*
    * This is right since buf is allocated on the heap. However
    * you will have to deallocate it later.
    */
   gtk_signal_connect (GTK_OBJECT (main_window),
                       "delete_event",
                       GTK_SIGNAL_FUNC (file_quit_cmd_callback),
                       buf);
   ....
}

Esteban Quijano V
Artinsoft corp


> -----Original Message-----
> From: Andreas Scherf [mailto:scherfa fh-trier de]
> Sent: Thursday, September 13, 2001 2:58 AM
> To: Esteban Quijano Vincenzi
> Cc: gtk-list gnome org
> Subject: Re: signal_connect ??
>
>
> > Esteban Quijano Vincenzi wrote:
> >
> > ok, if you see the API reference:
> >
> http://developer.gnome.org/doc/API/gtk/gtkwidget.html#GTKWIDGE
> T-DELETE-EVENT
> >
> > you will see that the callback function receives 3
> parameters instead
> > of two:
> >
> > WRONG:
> >
> > void
> > file_quit_cmd_callback (GtkWidget * widget, gpointer data)
> > {
> >   g_print ("%s\n", (gchar *) data);
> >   gtk_exit (0);
> > }
> >
> > RIGHT:
> >
> > void
> > file_quit_cmd_callback (GtkWidget * widget, GdkEvent
> *event, gpointer
> > data)
> > {
> >   g_print ("%s\n", (gchar *) data);
> >   gtk_exit (0);
> > }
> >
> > In the wrong code, you are interpreting the event pointer
> as a string.
> > Don't forget to put the middle parameter no matter if
> you're not gonna
> > use it.
> >
> > Hope it helps,
> >
> > Esteban Quijano Vincenzi
> > Artinsoft corp.
> But the compiler says :
> menubar.c:74: warning: passing arg 2 of `file_quit_cmd_callback' from
> incompatible pointer type
> menubar.c:74: too few arguments to function `file_quit_cmd_callback'
>
> Dont know if that could be the right thing ?
> Scherfy
> --
> ICQ: 52910964
> scherfa fh-trier de
> scherfa web de
>



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