macOS: Fix a timer inconsistency and prevent a crash

Calling Fl::repeat_timeout() instead of Fl::add_timeout() to create
a timer would crash on macOS but fall back to Fl::add_timeout() on
Windows and Unix/Linux. Although this is documented as "undefined
behavior" a crash should always be avoided and the fallback is now
consistent on all major platforms.

In the future this may be documented as the standard behavior.
This commit is contained in:
Albrecht Schlosser 2021-07-07 15:35:14 +02:00
parent 1008cdfab2
commit 87475c20d6

View File

@ -518,6 +518,10 @@ void Fl_Cocoa_Screen_Driver::add_timeout(double time, Fl_Timeout_Handler cb, voi
void Fl_Cocoa_Screen_Driver::repeat_timeout(double time, Fl_Timeout_Handler cb, void* data) void Fl_Cocoa_Screen_Driver::repeat_timeout(double time, Fl_Timeout_Handler cb, void* data)
{ {
if (!current_timer) {
add_timeout(time, cb, data);
return;
}
// k = how many times 'time' seconds after the last scheduled timeout until the future // k = how many times 'time' seconds after the last scheduled timeout until the future
double k = ceil( (CFAbsoluteTimeGetCurrent() - current_timer->next_timeout) / time); double k = ceil( (CFAbsoluteTimeGetCurrent() - current_timer->next_timeout) / time);
if (k < 1) k = 1; if (k < 1) k = 1;