如何从无效的 NSTimer 调试 EXC_BAD_ACCESS
我有一个重复的 NSTimer,在释放拥有计时器调用的回调的对象之前,我手动使其无效。我验证当我使计时器无效时,回调不再被调用。当对象释放时,立即我得到一个 EXC_BAD_ACCESS。崩溃与我是否使计时器无效相关,即如果我不使计时器无效,则不会发生崩溃。
有谁知道为什么会发生这种情况?我不明白为什么计时器会尝试访问已释放对象的回调,这似乎正在发生。我不知道如何进一步调试它。调用堆栈只是说:
#0 0x02f05c93 in objc_msgSend
#1 0x00000001 in ??
#2 0x02d255fa in CFRunLoopRunSpecific
#3 0x02d248a8 in CFRunLoopRunInMode
#4 0x037de89d in GSEventRunModal
#5 0x037de962 in GSEventRun
#6 0x00863372 in UIApplicationMain
#7 0x00002e40 in main at main.m:13
更新:我已经确定它不是计时器,而是由于我的父对象调用 dealloc 而导致的泄漏(非无效计时器阻止了 dealloc 调用)。当我用调用堆栈碰壁时,听到有关如何调试事物的建议仍然很有用,如果可能的话,所以我将保留这个问题。
I've got a repeating NSTimer that I invalidate manually some time before I dealloc the object that owns the callback that is invoked by the timer. I verify that when I invalidate the timer, the callback is not being called anymore. When the object deallocs, an instant later I get an EXC_BAD_ACCESS. The crash is correlated with whether I invalidate the timer or not, i.e. if I don't invalidate it, there is no crash.
Does anyone know why this might be happening? I don't see why the timer would try to access the callback for the dealloc'ed object, which is what seems to be happening. I'm at a loss how to debug this further. The callstack just says:
#0 0x02f05c93 in objc_msgSend
#1 0x00000001 in ??
#2 0x02d255fa in CFRunLoopRunSpecific
#3 0x02d248a8 in CFRunLoopRunInMode
#4 0x037de89d in GSEventRunModal
#5 0x037de962 in GSEventRun
#6 0x00863372 in UIApplicationMain
#7 0x00002e40 in main at main.m:13
UPDATE: I have determined it is not the timer but a leak resulting from my parent object calling dealloc (the non-invalidating timer was preventing dealloc from calling). It would still be useful to hear advice on how to debug things when I hit a wall with the callstack, if it is even possible, so I'll leave this question up.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您使
NSTimer
失效时,接收方会向timerWithTimeInterval 的
。您是否有可能引用这些对象或对这些对象之一进行了不正确的内存管理?target
和userInfo
参数发送release
:目标:选择器:用户信息:重复:调试此问题的一种方法是使用 NSZombie。
When you invalidate an
NSTimer
, the receiver sends arelease
to thetarget
anduserInfo
parameters oftimerWithTimeInterval:target:selector:userInfo:repeats:
. Is it possible you are referring to these objects or have some incorrect memory-management with one of these objects?One way to debug this is to make use of NSZombie.