如何从无效的 NSTimer 调试 EXC_BAD_ACCESS

发布于 2024-09-28 18:20:15 字数 623 浏览 1 评论 0原文

我有一个重复的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满身野味 2024-10-05 18:20:15

当您使 NSTimer 失效时,接收方会向 timerWithTimeInterval 的 targetuserInfo 参数发送 release :目标:选择器:用户信息:重复:。您是否有可能引用这些对象或对这些对象之一进行了不正确的内存管理?

调试此问题的一种方法是使用 NSZombie

When you invalidate an NSTimer, the receiver sends a release to the target and userInfo parameters of timerWithTimeInterval: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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文