Re: GVolumeMonitor, signals and scoping



On Sep 25, 2012, at 8:54 AM, Daniel Drake <dsd laptop org> wrote:

> Hi,
> 
> Take this test program:
> 
> ============
> from gi.repository import Gio, Gtk
> mon = None
> 
> def volume_added(mon, volume):
>        print "VOLUME ADDED", volume.get_name()
> 
> def start():
>        #global mon
>        mon = Gio.VolumeMonitor.get()
>        mon.connect("volume-added", volume_added)
> 
> start()
> Gtk.main()
> ============
> 
> It doesn't work - volume_added is never called.
> 
> Uncomment the line that says "global mon". Now it works.
> 
> I presume the first version doesn't work since the mon object falls
> out of scope and the object gets destroyed. But the equivalent code
> did work for some reason in pygtk 2.x.
> 
> I guess the above behaviour makes sense, but it touches upon something
> I've never been quite certain about: if a gobject (or some kind of Gtk
> object) has all its references dropped but there are still signal
> handlers connected, does gobject/gtk go ahead and destroy the object
> anyway?

Yes. A zero reference count is the sole criterion for destruction. You can force an object to run its first-stage destructor with run_dispose(), but that won't free the memory. It's provided to break reference loops. Signals are just a way of registering callbacks, and have nothing to do with this process.

Regards,
John Ralls


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