连续两次调用dispatch_source_create()时崩溃

Jim*_*mmy 2 objective-c grand-central-dispatch ios

考虑以下代码:

@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
Run Code Online (Sandbox Code Playgroud)

如果我使用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
Run Code Online (Sandbox Code Playgroud)

Cri*_*tik 5

正如他们所说,一张图片胜过一千个文字:

详细信息

基本上,您正在释放一个暂停的调度对象,这似乎是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);
Run Code Online (Sandbox Code Playgroud)