10

使用的黄金法则NSNotification似乎是

“在(或)被释放removeObserver之前调用”。observerobject

我正在处理一个未遵循此规则的代码库,但我找不到违规行为。我已经搜索了代码并确保每个addObserver都有匹配removeObserver但我仍然看到以下各种崩溃报告:

OS Version:      iPhone OS 5.0.1 (9A405)
Report Version:  104

Exception Type:  SIGSEGV
Exception Codes: SEGV_ACCERR at 0x8
Crashed Thread:  0

Thread 0 Crashed:
0   libobjc.A.dylib                     0x31516fbc objc_msgSend + 16
1   Foundation                          0x3195b50f __57-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke_0 + 19
2   CoreFoundation                      0x37a02577 ___CFXNotificationPost_block_invoke_0 + 71
3   CoreFoundation                      0x3798e0cf _CFXNotificationPost + 1407
4   Foundation                          0x318cf3fb -[NSNotificationCenter postNotificationName:object:userInfo:] + 67
5   UIKit                               0x34e5ee25 -[UIApplication _handleApplicationSuspend:eventInfo:] + 697
6   UIKit                               0x34deed17 -[UIApplication handleEvent:withNewEvent:] + 2031
7   UIKit                               0x34dee3bf -[UIApplication sendEvent:] + 55
8   UIKit                               0x34dedd2d _UIApplicationHandleEvent + 5809
9   GraphicsServices                    0x3750bdf3 PurpleEventCallback + 883
10  CoreFoundation                      0x37a0a553 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 39
11  CoreFoundation                      0x37a0a4f5 __CFRunLoopDoSource1 + 141
12  CoreFoundation                      0x37a09343 __CFRunLoopRun + 1371
13  CoreFoundation                      0x3798c4dd CFRunLoopRunSpecific + 301
14  CoreFoundation                      0x3798c3a5 CFRunLoopRunInMode + 105
15  GraphicsServices                    0x3750afcd GSEventRunModal + 157
16  UIKit                               0x34e1c743 UIApplicationMain + 1091
17  App                                 0x00002d2f main (main.m:14)

我对这个崩溃报告的解释[UIApplication _handleApplicationSuspend:eventInfo:]是发布一个通知,观察者在被删除之前已被释放。

假设这种解释是正确的,我将如何确定发布的是哪个通知?理想情况下,释放的对象类型是什么?

4

2 回答 2

6

您可以使用调试器命令设置符号断点-[NSNotificationCenter postNotificationName:object:userInfo:]并打印传递给它的第三个参数(第一个是NSNotificationCenter,第二个, )。_cmdpo

于 2012-02-19T11:58:11.637 回答
0

确定这一点的另一种方法是获取名称参数值(您为发布通知而传递的字符串)并验证观察该字符串/名称的任何对象是否设置为在对象生命周期的某个时间或在释放时删除观察。

于 2013-03-26T19:39:30.540 回答