1
0
mirror of https://github.com/bluekitchen/btstack.git synced 2025-03-26 02:37:41 +00:00

retain timer twice, don't free null timer

This commit is contained in:
matthias.ringwald 2011-03-16 10:43:01 +00:00
parent 212b0cc614
commit d0b5eda730

@ -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;
}