连续两次调用 dispatch_source_create() 时崩溃
Crash when calling dispatch_source_create() two times in a row
考虑以下代码:
@interface ViewController ()
@property (nonatomic, strong) dispatch_source_t source;
@end
@implementation ViewController
-(void)viewDidLoad {
[super viewDidLoad];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
self.source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
self.source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_resume(self.source);
dispatch_source_set_timer(self.source, dispatch_time(DISPATCH_TIME_NOW, 0.2 * NSEC_PER_SEC), 0.2 * NSEC_PER_SEC, 0.1 * NSEC_PER_SEC);
dispatch_source_set_event_handler(self.source, ^{
NSLog(@"%s",__func__);
});
}
@end
如果我使用 dispatch_source_create
创建相同的 DISPATCH_SOURCE_TYPE_TIMER
两次,应用程序将崩溃。为什么?
像上例那样创建一次和两次调度源有什么区别吗?
libdispatch.dylib`_dispatch_xref_dispose:
0x10015e174 <+0>: ldr w8, [x0, #48]
0x10015e178 <+4>: cmp w8, #2 ; =2
0x10015e17c <+8>: b.hs 0x10015e184 ; <+16>
0x10015e180 <+12>: ret
0x10015e184 <+16>: stp x20, x21, [sp, #-16]!
0x10015e188 <+20>: adrp x20, 41
0x10015e18c <+24>: add x20, x20, #3849 ; =3849
0x10015e190 <+28>: adrp x21, 46
0x10015e194 <+32>: add x21, x21, #2440 ; =2440
0x10015e198 <+36>: str x20, [x21]
0x10015e19c <+40>: ldp x20, x21, [sp], #16
-> 0x10015e1a0 <+44>: brk #0x1
俗话说,一图胜千言:
基本上你正在释放一个暂停的调度对象,它似乎是 prohibited 由 CGD。
恢复第一个计时器将使崩溃消失:
self.source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_resume(self.source);
self.source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_resume(self.source);
考虑以下代码:
@interface ViewController ()
@property (nonatomic, strong) dispatch_source_t source;
@end
@implementation ViewController
-(void)viewDidLoad {
[super viewDidLoad];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
self.source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
self.source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_resume(self.source);
dispatch_source_set_timer(self.source, dispatch_time(DISPATCH_TIME_NOW, 0.2 * NSEC_PER_SEC), 0.2 * NSEC_PER_SEC, 0.1 * NSEC_PER_SEC);
dispatch_source_set_event_handler(self.source, ^{
NSLog(@"%s",__func__);
});
}
@end
如果我使用 dispatch_source_create
创建相同的 DISPATCH_SOURCE_TYPE_TIMER
两次,应用程序将崩溃。为什么?
像上例那样创建一次和两次调度源有什么区别吗?
libdispatch.dylib`_dispatch_xref_dispose:
0x10015e174 <+0>: ldr w8, [x0, #48]
0x10015e178 <+4>: cmp w8, #2 ; =2
0x10015e17c <+8>: b.hs 0x10015e184 ; <+16>
0x10015e180 <+12>: ret
0x10015e184 <+16>: stp x20, x21, [sp, #-16]!
0x10015e188 <+20>: adrp x20, 41
0x10015e18c <+24>: add x20, x20, #3849 ; =3849
0x10015e190 <+28>: adrp x21, 46
0x10015e194 <+32>: add x21, x21, #2440 ; =2440
0x10015e198 <+36>: str x20, [x21]
0x10015e19c <+40>: ldp x20, x21, [sp], #16
-> 0x10015e1a0 <+44>: brk #0x1
俗话说,一图胜千言:
基本上你正在释放一个暂停的调度对象,它似乎是 prohibited 由 CGD。
恢复第一个计时器将使崩溃消失:
self.source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_resume(self.source);
self.source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_resume(self.source);