Re: strange gtk_set_data_full behavior ...



On Wed, 9 Jun 1999, Sven LUTHER wrote:

> On Wed, Jun 09, 1999 at 07:51:42AM -0400, Owen Taylor wrote:
> > > > The warning is indicating that you can't set a destroy function
> > > > for a NULL data pointer. (The reason for this is that
> > > > gtk_object_set_data (object, key, NULL) actually removes
> > > > that key from the list attached to the widget.
> > > 
> > > Why not just ignore the function then, and remove the data ? Why give an error
> > > for that ?
> > 
> > I think the reason that there is a warning there is so that
> > people won't expect the destroy function to be called if
> > that key is subsequently set again. Ignoring that argument
> 
> huh ?
> 
> if you reset the key, you have to provide a new destroy function, isn't it ? Or do

nope, a destroy function is always associated with the data portion for
a certain key. a data portion of NULL means that all previous data for
that key should be destroyed, in order to destroy the data that was
previously set, it will get invoked with its destroy function (if any
was specified), so it makes no sense at all to specify a destroy function
for the NULL data.

> you mean that the people expect the destroy function to be called with the NULL
> argument then ...

yes, which is obviously bogus.

> > #include <gtk/gtk.h>
> > #include <stdio.h>
> > 
> >  char data1[] = "Hello";
> >  char data2[] = "Bye";
> >  char data3[] = "XXX";
> >  
> > 
> > void sven_free (gpointer data)
> >  {
> >    printf ("Hello you %u.\n", data);
> >    printf ("Data is %s.\n", data);
> >  }
> > 
> > int main (int argc, char **argv)
> > {
> >   GtkWidget *window;
> > 
> >   gtk_init (&argc, &argv);
> > 
> >   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
> >  
> >   printf ("Data1 = %u.\n", data1);
> >   printf ("Data2 = %u.\n", data2);
> >   printf ("Data3 = %u.\n", data3);
> >   gtk_object_set_data_full (GTK_OBJECT (window), "Sven_data",\
> > 			    (gpointer)data1, (GtkDestroyNotify)&sven_free);
> >   gtk_object_set_data_full (GTK_OBJECT (window), "Sven_data",\
> > 			    (gpointer)data2, (GtkDestroyNotify)&sven_free);
> >   gtk_object_set_data_full (GTK_OBJECT (window), "Sven_data",\
> > 			    (gpointer)data3, (GtkDestroyNotify)&sven_free);
> >   gtk_object_set_data_full (GTK_OBJECT (window), "Sven_data",\
> > 			    (gpointer)NULL, (GtkDestroyNotify)&sven_free);
> > 
> >   gtk_widget_show (window);
> > 
> >   gtk_widget_destroy (window);
> > }
> 
> So the main difference is that the button press callback calls  gtk_exit (0);
> in my example.
> 
> I suppose gtk_exit exits without doing any cleanup after himself, while
> gtk_widget_destroy kills the widget cleanly ...

yes, configure and compile gtk with --enable-debug=yes and put
export GTK_DEBUG=objects in your .profile (or pass --gtk-debug=objects
to your gtk program) and gtk will inform you about stale objects
from an atexit handler.

> 
> Sven LUTHER
> 

---
ciaoTJ



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