8000 macosx: Clean up single-shot timers correctly · matplotlib/matplotlib@9eda24f · GitHub
[go: up one dir, main page]

Skip to content

Commit 9eda24f

Browse files
committed
macosx: Clean up single-shot timers correctly
The `NSTimer` docs state that a non-repeating (aka single-shot in our terms) timer is invalidated after it fires. This means that we should not do it ourselves, and in fact it appears that the pointer itself is no longer valid, so we would be passing an `invalidate` message to a random object or segfault.
1 parent 35c1dd2 commit 9eda24f

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

lib/matplotlib/backends/backend_macosx.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,9 @@ def draw_idle(self):
6767

6868
def _single_shot_timer(self, callback):
6969
"""Add a single shot timer with the given callback"""
70-
# We need to explicitly stop and remove the timer after
71-
# firing, otherwise segfaults will occur when trying to deallocate
72-
# the singleshot timers.
7370
def callback_func(callback, timer):
7471
callback()
7572
self._timers.remove(timer)
76-
timer.stop()
7773
timer = self.new_timer(interval=0)
7874
timer.single_shot = True
7975
timer.add_callback(callback_func, callback, timer)

src/_macosx.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,6 +1741,11 @@ - (void)flagsChanged:(NSEvent *)event
17411741
repeats: !single
17421742
block: ^(NSTimer *timer) {
17431743
gil_call_method((PyObject*)self, "_on_timer");
1744+
if (single) {
1745+
// A single-shot timer will be automatically invalidated when it fires, so
1746+
// we shouldn't do it ourselves when the object is deleted.
1747+
self->timer = NULL;
1748+
}
17441749
}];
17451750
// Schedule the timer on the main run loop which is needed
17461751
// when updating the UI from a background thread

0 commit comments

Comments
 (0)
0