diff --git a/src/run_loop_cocoa.m b/src/run_loop_cocoa.m index d28b517b7..9be247a29 100644 --- a/src/run_loop_cocoa.m +++ b/src/run_loop_cocoa.m @@ -113,21 +113,22 @@ void cocoa_add_timer(timer_source_t * ts) // note: ts uses unix time: seconds since Jan 1st 1970, CF uses Jan 1st 2001 as reference date // printf("kCFAbsoluteTimeIntervalSince1970 = %f\n", kCFAbsoluteTimeIntervalSince1970); CFAbsoluteTime fireDate = ((double)ts->timeout.tv_sec) + (((double)ts->timeout.tv_usec)/1000000.0) - kCFAbsoluteTimeIntervalSince1970; // unix time - since Jan 1st 1970 - // printf("cocoa_add_timer %x, now %f, then %f\n", (int) ts, CFAbsoluteTimeGetCurrent(),fireDate); CFRunLoopTimerContext timerContext = {0, ts, NULL, NULL, NULL}; CFRunLoopTimerRef timerRef = CFRunLoopTimerCreate (kCFAllocatorDefault,fireDate,0,0,0,theCFRunLoopTimerCallBack,&timerContext); + CFRetain(timerRef); // hack: store CFRunLoopTimerRef in next pointer of linked_item ts->item.next = (void *)timerRef; - // printf("cocoa_add_timer ref %x\n", (int) ts->item.next); + // printf("cocoa_add_timer %x -> %x now %f, then %f\n", (int) ts, (int) ts->item.next, CFAbsoluteTimeGetCurrent(),fireDate); CFRunLoopAddTimer(CFRunLoopGetCurrent(), timerRef, kCFRunLoopCommonModes); } int cocoa_remove_timer(timer_source_t * ts){ - // printf("cocoa_remove_timer ref %x\n", (int) ts->item.next); - // CFRunLoopRemoveTimer(CFRunLoopGetCurrent(), (CFRunLoopTimerRef) ts->item.next, kCFRunLoopCommonModes); - CFRunLoopTimerInvalidate((CFRunLoopTimerRef) ts->item.next); // also removes timer from run loops - CFRelease((CFRunLoopTimerRef) ts->item.next); + // printf("cocoa_remove_timer %x -> %x\n", (int) ts, (int) ts->item.next); + if (ts->item.next != NULL) { + CFRunLoopTimerInvalidate((CFRunLoopTimerRef) ts->item.next); // also removes timer from run loops + releases it + CFRelease((CFRunLoopTimerRef) ts->item.next); + } return 0; }