Skip to content

Commit

Permalink
Fix creating multiple timers to update song rows UI
Browse files Browse the repository at this point in the history
As it sits Pithos starts a new UI loop every time play is pressed
(maybe even more often?) and never destroys old loops. They stack up
over time creating a small memory leak of sorts. This patch makes
Pithos handle the UI loop more intelligently by starting/creating it
when needed and stopping/destroying it when it's not needed (like
when Pithos is paused). Credit for the code goes to the Exaile devs
as it's pretty much a copy and paste of their code with a little
tweaking to make it work with Pithos.

Closes #166
  • Loading branch information
JasonLG1979 authored and TingPing committed Mar 30, 2015
1 parent 098f7e1 commit cab2187
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions pithos/pithos.py
Expand Up @@ -245,7 +245,7 @@ def init_core(self):
self.gstreamer_error = ''
self.waiting_for_playlist = False
self.start_new_playlist = False

self.ui_loop_timer_id = 0
self.worker = GObjectWorker()
self.art_worker = GObjectWorker()

Expand Down Expand Up @@ -508,8 +508,8 @@ def user_play(self, *ignore):
def play(self):
if not self.playing:
self.playing = True
self.create_ui_loop()
self.player.set_state(Gst.State.PLAYING)
GLib.timeout_add_seconds(1, self.update_song_row)
self.playpause_image.set_from_icon_name('media-playback-pause-symbolic', Gtk.IconSize.SMALL_TOOLBAR)
self.update_song_row()
self.emit('play-state-changed', True)
Expand All @@ -520,6 +520,7 @@ def user_pause(self, *ignore):

def pause(self):
self.playing = False
self.destroy_ui_loop()
self.player.set_state(Gst.State.PAUSED)
self.playpause_image.set_from_icon_name('media-playback-start-symbolic', Gtk.IconSize.SMALL_TOOLBAR)
self.update_song_row()
Expand All @@ -534,6 +535,7 @@ def stop(self):
self.emit("song-ended", prev)

self.playing = False
self.destroy_ui_loop()
self.player.set_state(Gst.State.NULL)
self.emit('play-state-changed', False)

Expand Down Expand Up @@ -845,6 +847,15 @@ def update_song_row(self, song = None):
self.songs_model[song.index][2] = self.song_icon(song) or ""
return self.playing

def create_ui_loop(self):
if not self.ui_loop_timer_id:
self.ui_loop_timer_id = GLib.timeout_add_seconds(1, self.update_song_row)

def destroy_ui_loop(self):
if self.ui_loop_timer_id:
GLib.source_remove(self.ui_loop_timer_id)
self.ui_loop_timer_id = 0

def stations_combo_changed(self, widget):
index = widget.get_active()
if index>=0:
Expand Down

0 comments on commit cab2187

Please sign in to comment.