#5532: Fix memory leak when a buddy disappears.

This commit is contained in:
Tomeu Vizoso 2008-01-09 21:21:06 +01:00
parent 86b943b244
commit 83353f3c72
7 changed files with 64 additions and 53 deletions

View File

@ -423,6 +423,11 @@ class CanvasIcon(hippo.CanvasBox, hippo.CanvasItem):
hippo.CanvasBox.__init__(self, **kwargs) hippo.CanvasBox.__init__(self, **kwargs)
self._palette = None self._palette = None
self.connect('destroy', self.__destroy_cb)
def __destroy_cb(self, icon):
if self._palette is not None:
self._palette.destroy()
def do_set_property(self, pspec, value): def do_set_property(self, pspec, value):
if pspec.name == 'file-name': if pspec.name == 'file-name':

View File

@ -148,6 +148,7 @@ class Palette(gtk.Window):
# Just assume xthickness and ythickness are the same # Just assume xthickness and ythickness are the same
self.set_border_width(self.style.xthickness) self.set_border_width(self.style.xthickness)
self.connect('realize', self._realize_cb) self.connect('realize', self._realize_cb)
self.connect('destroy', self.__destroy_cb)
self.palette_state = self.PRIMARY self.palette_state = self.PRIMARY
@ -217,6 +218,12 @@ class Palette(gtk.Window):
self._mouse_detector = MouseSpeedDetector(self, 200, 5) self._mouse_detector = MouseSpeedDetector(self, 200, 5)
self._mouse_detector.connect('motion-slow', self._mouse_slow_cb) self._mouse_detector.connect('motion-slow', self._mouse_slow_cb)
def __destroy_cb(self, palette):
self.set_group_id(None)
if self._palette_popup_sid is not None:
_palette_observer.disconnect(self._palette_popup_sid)
def _add_menu(self): def _add_menu(self):
self._menu_box = gtk.VBox() self._menu_box = gtk.VBox()
self._secondary_box.pack_start(self._menu_box) self._secondary_box.pack_start(self._menu_box)

View File

@ -68,6 +68,7 @@ class Group(gobject.GObject):
palette.disconnect(sid) palette.disconnect(sid)
self._palettes.remove(palette) self._palettes.remove(palette)
del self._sig_ids[palette]
def popdown(self): def popdown(self):
for palette in self._palettes: for palette in self._palettes:

View File

@ -78,11 +78,16 @@ class Buddy(gobject.GObject):
bobj = bus.get_object(self._PRESENCE_SERVICE, object_path) bobj = bus.get_object(self._PRESENCE_SERVICE, object_path)
self._buddy = dbus.Interface(bobj, self._BUDDY_DBUS_INTERFACE) self._buddy = dbus.Interface(bobj, self._BUDDY_DBUS_INTERFACE)
self._buddy.connect_to_signal('IconChanged', self._icon_changed_cb,
byte_arrays=True) self._icon_changed_signal = self._buddy.connect_to_signal(
self._buddy.connect_to_signal('JoinedActivity', self._joined_activity_cb) 'IconChanged', self._icon_changed_cb, byte_arrays=True)
self._buddy.connect_to_signal('LeftActivity', self._left_activity_cb) self._joined_activity_signal = self._buddy.connect_to_signal(
self._buddy.connect_to_signal('PropertyChanged', self._property_changed_cb) 'JoinedActivity', self._joined_activity_cb)
self._left_activity_signal = self._buddy.connect_to_signal(
'LeftActivity', self._left_activity_cb)
self._property_changed_signal = self._buddy.connect_to_signal(
'PropertyChanged', self._property_changed_cb)
self._properties = self._get_properties_helper() self._properties = self._get_properties_helper()
activities = self._buddy.GetJoinedActivities() activities = self._buddy.GetJoinedActivities()
@ -90,6 +95,12 @@ class Buddy(gobject.GObject):
self._activities[op] = self._ps_new_object(op) self._activities[op] = self._ps_new_object(op)
self._icon = None self._icon = None
def destroy(self):
self._icon_changed_signal.remove()
self._joined_activity_signal.remove()
self._left_activity_signal.remove()
self._property_changed_signal.remove()
def _get_properties_helper(self): def _get_properties_helper(self):
"""Retrieve the Buddy's property dictionary from the service object """Retrieve the Buddy's property dictionary from the service object
""" """

View File

@ -156,10 +156,10 @@ class PresenceService(gobject.GObject):
returns presence Buddy or Activity representation returns presence Buddy or Activity representation
""" """
_logger.debug('Creating proxy for %s', object_path)
obj = None obj = None
try: try:
obj = self._objcache[object_path] obj = self._objcache[object_path]
_logger.debug('Reused proxy %r', obj)
except KeyError: except KeyError:
if object_path.startswith(self._PS_BUDDY_OP): if object_path.startswith(self._PS_BUDDY_OP):
obj = Buddy(self._bus, self._new_object, obj = Buddy(self._bus, self._new_object,
@ -175,7 +175,7 @@ class PresenceService(gobject.GObject):
else: else:
raise RuntimeError("Unknown object type") raise RuntimeError("Unknown object type")
self._objcache[object_path] = obj self._objcache[object_path] = obj
_logger.debug('Proxy is %r', obj) _logger.debug('Created proxy %r', obj)
return obj return obj
def _have_object(self, object_path): def _have_object(self, object_path):
@ -200,7 +200,17 @@ class PresenceService(gobject.GObject):
# Don't try to create a new object here if needed; it will probably # Don't try to create a new object here if needed; it will probably
# fail anyway because the object has already been destroyed in the PS # fail anyway because the object has already been destroyed in the PS
if self._have_object(object_path): if self._have_object(object_path):
self.emit('buddy-disappeared', self._new_object(object_path)) obj = self._objcache[object_path]
self.emit('buddy-disappeared', obj)
# We cannot maintain the object in the cache because that would keep
# a lot of objects from being collected. That includes UI objects
# due to signals using strong references.
# If we want to cache some despite the memory usage increase,
# we could use a LRU cache limited to some value.
del self._objcache[object_path]
obj.destroy()
return False return False
def _buddy_disappeared_cb(self, object_path): def _buddy_disappeared_cb(self, object_path):

View File

@ -16,7 +16,6 @@
from gettext import gettext as _ from gettext import gettext as _
import logging import logging
#import gtk
import gobject import gobject
import hippo import hippo
@ -31,55 +30,31 @@ class BuddyMenu(Palette):
self._shell = shell self._shell = shell
Palette.__init__(self, buddy.get_nick()) Palette.__init__(self, buddy.get_nick())
self._active_activity_changed_hid = None
# FIXME: re-enable when buddy avatars are re-enabled self.connect('destroy', self.__destroy_cb)
# pixbuf = None
# try:
# pixbuf = self._get_buddy_icon_pixbuf()
# except gobject.GError, e:
# pass
# if pixbuf:
# scaled_pixbuf = pixbuf.scale_simple(units.grid_to_pixels(1),
# units.grid_to_pixels(1),
# gtk.gdk.INTERP_BILINEAR)
# del pixbuf
# image = gtk.Image()
# image.set_from_pixbuf(scaled_pixbuf)
# self.set_content(image)
# image.show()
self._buddy.connect('icon-changed', self._buddy_icon_changed_cb) self._buddy.connect('icon-changed', self._buddy_icon_changed_cb)
self._buddy.connect('nick-changed', self._buddy_nick_changed_cb) self._buddy.connect('nick-changed', self._buddy_nick_changed_cb)
owner = shell.get_model().get_owner() owner = self._get_shell_model().get_owner()
if buddy.get_nick() != owner.get_nick(): if buddy.get_nick() != owner.get_nick():
self._add_items() self._add_items()
# FIXME: re-enable when buddy avatars are re-enabled def _get_shell_model(self):
# def _get_buddy_icon_pixbuf(self): return self._shell.get_model()
# buddy_object = self._buddy.get_buddy()
# if not buddy_object: def _get_home_model(self):
# return None return self._get_shell_model().get_home()
#
# icon_data = buddy_object.props.icon def __destroy_cb(self, menu):
# if not icon_data: if self._active_activity_changed_hid is not None:
# return None home_model = self._get_home_model()
# pbl = gtk.gdk.PixbufLoader() home_model.disconnect(self._active_activity_changed_hid)
# pbl.write(icon_data)
# pixbuf = None
# try:
# pbl.close()
# pixbuf = pbl.get_pixbuf()
# except gobject.GError:
# pass
# del pbl
# return pixbuf
def _add_items(self): def _add_items(self):
shell_model = self._shell.get_model()
pservice = presenceservice.get_instance() pservice = presenceservice.get_instance()
friends = shell_model.get_friends() friends = self._get_shell_model().get_friends()
if friends.has_buddy(self._buddy): if friends.has_buddy(self._buddy):
menu_item = MenuItem(_('Remove friend'), 'list-remove') menu_item = MenuItem(_('Remove friend'), 'list-remove')
menu_item.connect('activate', self._remove_friend_cb) menu_item.connect('activate', self._remove_friend_cb)
@ -94,9 +69,9 @@ class BuddyMenu(Palette):
self._invite_menu.connect('activate', self._invite_friend_cb) self._invite_menu.connect('activate', self._invite_friend_cb)
self.menu.append(self._invite_menu) self.menu.append(self._invite_menu)
home_model = shell_model.get_home() home_model = self._get_home_model()
home_model.connect('active-activity-changed', self._active_activity_changed_hid = home_model.connect(
self._cur_activity_changed_cb) 'active-activity-changed', self._cur_activity_changed_cb)
activity = home_model.get_active_activity() activity = home_model.get_active_activity()
self._update_invite_menu(activity) self._update_invite_menu(activity)
@ -125,11 +100,11 @@ class BuddyMenu(Palette):
self.set_primary_text(nick) self.set_primary_text(nick)
def _make_friend_cb(self, menuitem): def _make_friend_cb(self, menuitem):
friends = self._shell.get_model().get_friends() friends = self._get_shell_model().get_friends()
friends.make_friend(self._buddy) friends.make_friend(self._buddy)
def _remove_friend_cb(self, menuitem): def _remove_friend_cb(self, menuitem):
friends = self._shell.get_model().get_friends() friends = self._get_shell_model().get_friends()
friends.remove(self._buddy) friends.remove(self._buddy)
def _invite_friend_cb(self, menuitem): def _invite_friend_cb(self, menuitem):

View File

@ -317,8 +317,8 @@ class ActivityView(hippo.CanvasBox):
def remove_buddy_icon(self, key): def remove_buddy_icon(self, key):
icon = self._icons[key] icon = self._icons[key]
self.remove(icon)
del self._icons[key] del self._icons[key]
icon.destroy()
def _clicked_cb(self, item): def _clicked_cb(self, item):
bundle_id = self._model.get_bundle_id() bundle_id = self._model.get_bundle_id()
@ -535,6 +535,7 @@ class MeshBox(hippo.CanvasBox):
icon = self._buddies[buddy_model.get_key()] icon = self._buddies[buddy_model.get_key()]
self._layout.remove(icon) self._layout.remove(icon)
del self._buddies[buddy_model.get_key()] del self._buddies[buddy_model.get_key()]
icon.destroy()
def _remove_buddy(self, buddy_model): def _remove_buddy(self, buddy_model):
key = buddy_model.get_key() key = buddy_model.get_key()
@ -575,6 +576,7 @@ class MeshBox(hippo.CanvasBox):
icon = self._activities[activity_model.get_id()] icon = self._activities[activity_model.get_id()]
self._layout.remove(icon) self._layout.remove(icon)
del self._activities[activity_model.get_id()] del self._activities[activity_model.get_id()]
icon.destroy()
def _add_access_point(self, ap_model): def _add_access_point(self, ap_model):
meshdev = self._model.get_mesh() meshdev = self._model.get_mesh()