Skip to content

macosx: Clean up single-shot timers correctly #27875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions lib/matplotlib/backends/backend_macosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,9 @@ def draw_idle(self):

def _single_shot_timer(self, callback):
"""Add a single shot timer with the given callback"""
# We need to explicitly stop and remove the timer after
# firing, otherwise segfaults will occur when trying to deallocate
# the singleshot timers.
def callback_func(callback, timer):
callback()
self._timers.remove(timer)
timer.stop()
timer = self.new_timer(interval=0)
timer.single_shot = True
timer.add_callback(callback_func, callback, timer)
Expand Down
5 changes: 5 additions & 0 deletions src/_macosx.m
Original file line number Diff line number Diff line change
Expand Up @@ -1741,6 +1741,11 @@ - (void)flagsChanged:(NSEvent *)event
repeats: !single
block: ^(NSTimer *timer) {
gil_call_method((PyObject*)self, "_on_timer");
if (single) {
// A single-shot timer will be automatically invalidated when it fires, so
// we shouldn't do it ourselves when the object is deleted.
self->timer = NULL;
}
}];
// Schedule the timer on the main run loop which is needed
// when updating the UI from a background thread
Expand Down