Skip to content

Commit 922b699

Browse files
committed
Effort to reduce memory leaking from re-loading media-heavy documents
1 parent efc7899 commit 922b699

File tree

5 files changed

+39
-1
lines changed

5 files changed

+39
-1
lines changed

pympress/extras.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ def purge_media_overlays(self):
263263
""" Remove current media overlays.
264264
"""
265265
self.remove_media_overlays()
266+
for (v_da_c, v_da_p) in self._media_overlays.values():
267+
v_da_c.unload()
268+
v_da_p.unload()
266269
self._media_overlays.clear()
267270

268271

pympress/media_overlays/base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@ def is_playing(self):
236236
raise NotImplementedError
237237

238238

239+
def unload(self):
240+
""" Unload the media file and tear down playback mechanisms.
241+
"""
242+
raise NotImplementedError
243+
244+
239245
def do_stop(self):
240246
""" Stops playing in the backend player.
241247
"""

pympress/media_overlays/gif_backend.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class GifOverlay(base.VideoOverlay):
4646
base_size = None
4747
#: The :class:`~cairo.Matrix` defining the zoom & shift to scale the gif
4848
transform = None
49+
#: `int` tracking the callback for the next redraw
50+
loop = -1
4951

5052
def __init__(self, *args, **kwargs):
5153
super(GifOverlay, self).__init__(*args, **kwargs)
@@ -59,6 +61,15 @@ def __init__(self, *args, **kwargs):
5961
self.movie_zone.connect('configure-event', self.set_transform)
6062

6163

64+
def unload(self):
65+
""" Unload the media file and tear down playback mechanisms.
66+
"""
67+
if self.loop >= 0:
68+
GLib.Source.remove(self.loop)
69+
self.loop = -1
70+
self.anim_iter = None
71+
self.anim = None
72+
6273
def _set_file(self, filepath):
6374
""" Sets the media file to be played by the widget.
6475
@@ -101,12 +112,18 @@ def draw(self, widget, ctx):
101112
def advance_gif(self):
102113
""" Advance the gif, queue redrawing if the frame changed, and schedule the next frame.
103114
"""
115+
if self.anim_iter is None:
116+
return False
117+
104118
if self.anim_iter.advance():
105119
self.movie_zone.queue_draw()
106120

107121
delay = self.anim_iter.get_delay_time()
108122
if delay >= 0:
109-
GLib.timeout_add(delay, self.advance_gif)
123+
self.loop = GLib.timeout_add(interval=delay, function=self.advance_gif)
124+
else:
125+
self.loop = -1
126+
return False
110127

111128

112129
def do_set_time(self, t):

pympress/media_overlays/gst_backend.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@ def do_stop(self):
195195
return False
196196

197197

198+
def unload(self):
199+
""" Unload the media file and tear down playback mechanisms.
200+
"""
201+
pass
202+
203+
198204
def do_set_time(self, time):
199205
""" Set the player at time `~time`.
200206

pympress/media_overlays/vlc_backend.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,12 @@ def do_stop(self):
202202
self.player.stop()
203203

204204

205+
def unload(self):
206+
""" Unload the media file and tear down playback mechanisms.
207+
"""
208+
pass
209+
210+
205211
def time_changed(self, event):
206212
""" Handle time passing
207213

0 commit comments

Comments
 (0)