Re: [gtk-list] Re: problem with gtk_signal_connect_object



On 19 May 1998, Owen Taylor wrote:

> 
> Stefan Wille <wille@netlife.de> writes:
> 
> > robert havoc pennington wrote:
> >  
> > > gtk_signal_connect_object() doesn't actually do anything to the
> > > "receiver," I don't think, you could actually use any pointer you want
> > > there, or even NULL. The pointer is simply stored and passed to the
> > > callback.
> > 
> > Yes, it seems to be like that. But I think it should be different, 
> > gtk should take care connections to deleted objects,
> > since it knows that the connection goes to a GtkObject.
> > 
> > > If you need to remove a callback, you can save the return value of
> > > gtk_signal_connect and use that to remove the callback at a later time.
> > 
> > I could do this, but I think gtk should do that for me. In Qt (don't
> > flame me)
> > it works the same - if you connect to an object and then delete the
> > object,
> > the connection gets disconnected.
> 
> I think you are assumming to much about gtk_signal_connect_object().
> 
> It doesn't do any special "object" magic for its fourth parameter.
> 
> guint  gtk_signal_connect_object	  (GtkObject	       *object,
> 					   const gchar	       *name,
> 					   GtkSignalFunc	func,
> 					   GtkObject	       *slot_object);
> 
> simply means is pass slot_object as the first parameter to func, instead
> of object.
> 
> You can currently write:
> 
>  gtk_signal_connect_object (object, "destroy",
>                             GTK_SIGNAL_FUNC (g_free),
>                             GTK_OBJECT (some_data_associated_with_object));
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                              this should have been
                              (GtkObject*) some_data_associated_with_object

otherwise you'll get conversion warnings if some_data_associated_with_object
is not derived from GTK_TYPE_OBJECT.

> So changing this could (and probably would) break existing code. 

it'd for sure break the gimp ;)

> Also, the extra bookkeeeping for keeping track of _object()
> connections I don't think is worth the benefit of the rare case when
> want destroy the object early.
> 
> Currently, you just have to write:
> 
>  gtk_signal_connect_object (GTK_OBJECT (object1), "signal",
>                             GTK_SIGNAL_FUNC (callback), GTK_OBJECT (object2));
>  gtk_signal_connect (GTK_OBJECT (object2), "destroy",
>                      GTK_SIGNAL_FUNC (gtk_signal_disconnect_by_data),
>                      object1);

this will cause problems if object1 is destroyed prior to object2, since upon
destruction of object2, gtk_signal_disconnect_by_data will operate on object1
which is already destroyed. that's why i added the *_while_alive connection
functions somewhile back.

but, regardless of the _while_alive functions, to get your example to work,
you would need to do:

gtk_signal_connect_object (GTK_OBJECT (object1),
                           "signal",
                           GTK_SIGNAL_FUNC (callback),
                           GTK_OBJECT (object2));
gtk_signal_connect (GTK_OBJECT (object2),
                    "destroy",
                    GTK_SIGNAL_FUNC (gtk_signal_disconnect_by_data),
                    object1);
gtk_signal_connect (GTK_OBJECT (object1),
                    "destroy",
                    GTK_SIGNAL_FUNC (gtk_signal_disconnect_by_data),
                    object2);

but this is overly complicated and can disconnect else signal handlers that
have data==object1 or data==object2 and would use it for other purposes (though
that is extremely unlikely).

> 
> Regards,
>                                         Owen
> 

---
ciaoTJ



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