微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我们谴责NSTimer无效时遇到了什么?

如何解决我们谴责NSTimer无效时遇到了什么?

self.timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timeCall:) userInfo:self.strong repeats:YES];
[self.timer invalidate];
NSLog(@"%p",[self.timer userInfo]);

然后,由于EXC_BAD_ACCESS,应用崩溃了。

Apple文档说,

This method is the only way to remove a timer from an NSRunLoop object. The NSRunLoop object removes its strong reference to the timer,either just before the invalidate method returns or at some later point. If it was configured with target and user info objects,the receiver removes its strong references to those objects as well.

Apple告诉我们在计时器失效后不要使用userInfo。为什么?

计时器的计数由程序员控制,还有userInfo(我尝试使用属性作为userInfo,而不是使用常量字符串),结果是相同的。

如果我们将符号断点放置为-[NSTimer userinfo]之类,我们可以获得崩溃信息,例如

CoreFoundation`-[__NSCFTimer userInfo]:
    0x7fff20384d32 <+0>:  pushq  %rbp
    0x7fff20384d33 <+1>:  movq   %rsp,%rbp
    0x7fff20384d36 <+4>:  pushq  %rbx
    0x7fff20384d37 <+5>:  subq   $0x38,%rsp
    0x7fff20384d3b <+9>:  movq   0x5fca7d6e(%rip),%rax    ; (void *)0x00007fff86d060e0: __stack_chk_guard
    0x7fff20384d42 <+16>: movq   (%rax),%rax
    0x7fff20384d45 <+19>: movq   %rax,-0x10(%rbp)
    0x7fff20384d49 <+23>: leaq   -0x38(%rbp),%rbx
    0x7fff20384d4d <+27>: movq   $0x0,(%rbx)
    0x7fff20384d54 <+34>: movq   %rbx,%rsi
    0x7fff20384d57 <+37>: callq  0x7fff203a6ff9            ; CFRunLoopTimerGetContext
    0x7fff20384d5c <+42>: movq   0x8(%rbx),%rax
->  0x7fff20384d60 <+46>: movq   0x18(%rax),%rax
    0x7fff20384d64 <+50>: movq   0x5fca7d45(%rip),%rcx    ; (void *)0x00007fff86d060e0: __stack_chk_guard
    0x7fff20384d6b <+57>: movq   (%rcx),%rcx
    0x7fff20384d6e <+60>: cmpq   -0x10(%rbp),%rcx
    0x7fff20384d72 <+64>: jne    0x7fff20384d7b            ; <+73>
    0x7fff20384d74 <+66>: addq   $0x38,%rsp
    0x7fff20384d78 <+70>: popq   %rbx
    0x7fff20384d79 <+71>: popq   %rbp
    0x7fff20384d7a <+72>: retq   
    0x7fff20384d7b <+73>: callq  0x7fff204c318e            ; symbol stub for: __stack_chk_fail

我知道NSRunloop和NSTimer之间的关系,但是这个userinfo属性呢?我想也许是为了存档readonly关键字。我们如何研究NSTimer的内部实现?有办法探索吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。