psychopy/psychopy — pull request #456 by frankpapenmeier (2013-12-12 10:32:32+00:00)
Title: BF: Fixed memory leak in visual.MovieStim

Two problems caused a memory leak when creating multiple MovieStim
instances across the experiment:

1.)
The line "self._player._on_eos=self._onEos" creates a reference to the
MovieStim object that prevents it from being garbage collected, that is
__del__ was never called

Solution:
set _on_eos only on playback and reset it on movie pause, stop and eos

2.)
The __del__ function did not release the memory occupied by the movie.py

Solution:
replace self._clearTextures() (which is not defined in MovieStim) by
self._player.next() (which releases the memory)

#####
Test cases to compare the old and new behavior:

First, add a "print 'del'" line to the MovieStim __del__ function to see that it
is not called. Additionally, watch the memory consumption of the psychopy
process

1.)
Realisitc test case (Memory adds up over time, takes a while until python
crashes because of a "MemoryError").. I guess any .avi movie should do.

from psychopy import visual

window = visual.Window()
for i in range(0,500):
video_stimulus = visual.MovieStim(window, "movie_memory_leak_demo.avi")
while video_stimulus.status != visual.FINISHED:
video_stimulus.draw()
window.flip()
window.close()

2.)
Faster test case which will quickly result in a "MemoryError"

from psychopy import visual

window = visual.Window()
for i in range(0,500):
video_stimulus = visual.MovieStim(window, "movie_memory_leak_demo.avi")
window.close()