From 2419071b0fcaa834e4abfcc454e253d7722c6ca0 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Wed, 12 Nov 2014 20:19:26 +0100 Subject: [PATCH] Disconnect destroyed signal of objects deleted in objreg. Fixes #205. --- qutebrowser/utils/objreg.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/qutebrowser/utils/objreg.py b/qutebrowser/utils/objreg.py index b9715a960..a9a265590 100644 --- a/qutebrowser/utils/objreg.py +++ b/qutebrowser/utils/objreg.py @@ -58,8 +58,15 @@ class ObjectRegistry(collections.UserDict): """A registry of long-living objects in qutebrowser. Inspired by the eric IDE code (E5Gui/E5Application.py). + + Attributes: + _partial_objs: A dictionary of the connected partial objects. """ + def __init__(self): + super().__init__() + self._partial_objs = {} + def __setitem__(self, name, obj): """Register an object in the object registry. @@ -70,10 +77,28 @@ class ObjectRegistry(collections.UserDict): if obj is None: raise TypeError("Registering object None with name '{}'!".format( name)) + + self._disconnect_destroyed(name) + if isinstance(obj, QObject): - obj.destroyed.connect(functools.partial(self.on_destroyed, name)) + func = functools.partial(self.on_destroyed, name) + obj.destroyed.connect(func) + self._partial_objs[name] = func + super().__setitem__(name, obj) + def __delitem__(self, name): + """Extend __delitem__ to disconnect the destroyed signal.""" + self._disconnect_destroyed(name) + super().__delitem__(name) + + def _disconnect_destroyed(self, name): + """Disconnect the destroyed slot if it was connected.""" + if name in self._partial_objs: + func = self._partial_objs[name] + self[name].destroyed.disconnect(func) + del self._partial_objs[name] + def on_destroyed(self, name): """Schedule removing of a destroyed QObject. @@ -89,6 +114,7 @@ class ObjectRegistry(collections.UserDict): log.misc.debug("removed: {}".format(name)) try: del self[name] + del self._partial_objs[name] except KeyError: pass