Repository: psychopy/psychopy

Issue:
Segmentation fault caused by clearTextures()
## Problem

In PsychoPy3, when `stop()` was called to stop MovieStim3, Python crashed due to segmentation fault. I tried to use `faulthandler` to find the problem, it turned out to be `clearTextures()`. Below is the error message in the prompt:

```bash
Fatal Python error: Segmentation fault

Thread 0x00007fa748987700 (most recent call first):

Current thread 0x00007fa74fb7c740 (most recent call first):
  File "/home/yu-han/envs/psy3py3/lib/python3.6/site-packages/psychopy/visual/basevisual.py", line 1001 in clearTextures
  File "/home/yu-han/NTU/cross-situational-learning/experiment_infant_1/movie3demo.py", line 13 in _unload
  File "/home/yu-han/envs/psy3py3/lib/python3.6/site-packages/psychopy/visual/movie3.py", line 264 in stop
  File "/home/yu-han/NTU/cross-situational-learning/experiment_infant_1/movie3demo.py", line 53 in <module>
Segmentation fault (core dumped)
```

I never had the error in previous version of PsychoPy. I'm trying to figure out what caused the error :thinking: 

## Example code
I used the same script in #1933 . The error only occurred in PsychoPy3,  it worked smoothly in v1.90.3. 
If `clearTextures()` was removed from `_unload()`, the error disappeared.

### To reproduce the error

```python
from psychopy import visual, core, event, prefs
prefs.general['audioLib'] = ['sounddevice']
from psychopy import sound

import faulthandler

faulthandler.enable()

win = visual.Window(
    size=[1280, 1024],
    allowGUI=False)

attention_grabber = visual.MovieStim3(
    win,
    "infant/elmo's ducks.mp4",
    size=[640, 480],
    units='pix',
    autoLog=False)

music = sound.Sound('infant/Rainy_Day_Games.wav', sampleRate=44100, stereo=True, loops=-1)

att_timer = core.CountdownTimer(attention_grabber.duration)
playing = True
attention_grabber.play()
att_timer.reset()
while att_timer.getTime() > 0 and playing:
    attention_grabber.draw()
    for key in event.getKeys():
        if key == 'escape' or key == 'space':
            playing = False
            break
    win.flip()

attention_grabber.stop()

event.clearEvents()

music_timer = core.CountdownTimer(music.duration)
playing = True
music.play()
music_timer.reset()
while music_timer.getTime() > 0 and playing:
    for key in event.getKeys():
        if key == 'escape' or key == 'space':
            playing = False
            break
    win.flip()
music.stop()

win.close()
core.quit()
```

## Versions

- Linux: 4.18.3/ Ubuntu 18.04
- PsychoPy: 3.0.0b6
- Python: 3.6.5

## Update

The error is caused by calling `GL.glDeleteTextures(1, self._texID)` when `self._texID` is `None`.

See the comment below for details


Discussion:

Resolving patch:
diff --git a/psychopy/visual/movie3.py b/psychopy/visual/movie3.py
--- a/psychopy/visual/movie3.py
+++ b/psychopy/visual/movie3.py
@@ -257,11 +257,12 @@ def stop(self, log=True):
         """
         if self.status != STOPPED:
             self.status = STOPPED
+            self._unload()
             self.reset()
             if log and self.autoLog:
                 self.win.logOnFlip("Set %s stopped" % (self.name),
                                    level=logging.EXP, obj=self)
-            self._unload()
+
 
     def setVolume(self, volume):
         pass  # to do


Test patch: