The golden rule of using NSNotification seems to be
"call removeObserver before the observer (or the object) is deallocated".
I'm dealing with a codebase where this rule hasn't been followed, but I can't locate the transgression. I've searched through the code and ensured that every addObserver has a matching removeObserver but I'm still seeing crash reports of the following variety:
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)
My interpretation of this crash report is that [UIApplication _handleApplicationSuspend:eventInfo:] is posting a notification for which an observer has been deallocated before being removed.
Assuming this interpretations is correct, how would I go about determining which notification is being posted? And ideally, what the deallocated object type?
You can set a symbolic breakpoint in -[NSNotificationCenter postNotificationName:object:userInfo:] and print the third argument passed to it (the first being the NSNotificationCenter, the second, _cmd) using the debugger po command.
Another way to determine this is to get the name parameter value (the string you passed for posting the notification) and verify that any object that is observing that string/name is set to remove observation sometime in the objects lifecycle or as it is deallocating.
Related
I have a strage crash scenario to crash the app!
When you do a fresh install by running the app from Xcode; the application will run perfectly.
But I installed by current app from app store, then run the application, now everything is perfect till you click on home button, the application will crash at that moment! What's the problem? And How could I solve it?
UPDATED (This is the log):
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x6f687370
Triggered by Thread: 0
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 ??? 0x6f687370 0 + 1869116272
1 CoreFoundation 0x291ad93c _CFXNotificationPost + 1784
2 Foundation 0x29edd9b8 -[NSNotificationCenter postNotificationName:object:userInfo:] + 72
3 UIKit 0x2c99b7ae -[UIApplication _handleApplicationDectivationWithScene:shouldForceExit:transitionContext:completion:] + 882
4 UIKit 0x2c9a29f0 __88-[UIApplication _handleApplicationLifecycleEventWithScene:transitionContext:completion:]_block_invoke + 76
5 UIKit 0x2c9a299c -[UIApplication _handleApplicationLifecycleEventWithScene:transitionContext:completion:] + 360
6 UIKit 0x2c997c82 -[UIApplication scene:didUpdateWithDiff:transitionContext:completion:] + 478
7 FrontBoardServices 0x2f9a21f8 __80-[FBSSceneImpl updater:didUpdateSettings:withDiff:transitionContext:completion:]_block_invoke_2 + 40
8 FrontBoardServices 0x2f9b10e8 __31-[FBSSerialQueue performAsync:]_block_invoke + 12
9 CoreFoundation 0x2925f5b4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
10 CoreFoundation 0x2925e878 __CFRunLoopDoBlocks + 216
11 CoreFoundation 0x2925d3b2 __CFRunLoopRun + 1714
12 CoreFoundation 0x291ab620 CFRunLoopRunSpecific + 476
13 CoreFoundation 0x291ab432 CFRunLoopRunInMode + 106
14 GraphicsServices 0x305310a8 GSEventRunModal + 136
15 UIKit 0x2c796358 UIApplicationMain + 1440
16 MyNiceTestApp 0x000cae9e 0xc4000 + 28318
17 libdyld.dylib 0x371bfaae start + 2
Since there is not source code to look at my guess would be you have registered for
an observer for NSNotificationCenter and you have not removed the observer before the
application terminates.
This will cause you problems if an NSNotification was delivered to a deallocated
listener.
It appears one of UIApplicationDidEnterBackgroundNotification listeners is deallocated without unregistering from observing UIApplicationDidEnterBackgroundNotification notification
While analyzing our crash logs I discovered that a fairly common crash is one that does not even occur in one of my own classes. These are external crash logs and I can't therefore not reproduce the problem.
Since I can't debug the Framework I am wondering, what a practicable approach could be to tackle this issue.
The only clue I have is that SIGSEGV might be a segmentation fault or an invalid pointer. But like I said since my Classes do not seem to be directly involved this does not help me much.
The Crash:
Exception Type: SIGSEGV
Exception Codes: SEGV_ACCERR at 0x6b636f6c
Crashed Thread: 0
Thread 0 Crashed:
0 libobjc.A.dylib 0x365c7f94 objc_msgSend + 43
1 CoreFoundation 0x31cd23fd -[NSObject performSelector:withObject:withObject:] + 52
2 UIKit 0x3291cfaf -[UIApplication sendAction:to:from:forEvent:] + 62
3 UIKit 0x3291cf6b -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 30
4 UIKit 0x3291cf49 -[UIControl sendAction:to:forEvent:] + 44
5 UIKit 0x3291ccb9 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 492
6 UIKit 0x3291d5f1 -[UIControl touchesEnded:withEvent:] + 476
7 UIKit 0x3291bad3 -[UIWindow _sendTouchesForEvent:] + 318
8 UIKit 0x3291b4c1 -[UIWindow sendEvent:] + 380
9 UIKit 0x3290183d -[UIApplication sendEvent:] + 356
10 UIKit 0x329010e3 _UIApplicationHandleEvent + 5826
11 GraphicsServices 0x3572d22b PurpleEventCallback + 882
12 CoreFoundation 0x31d4c523 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 38
13 CoreFoundation 0x31d4c4c5 __CFRunLoopDoSource1 + 140
14 CoreFoundation 0x31d4b313 __CFRunLoopRun + 1370
15 CoreFoundation 0x31cce4a5 CFRunLoopRunSpecific + 300
16 CoreFoundation 0x31cce36d CFRunLoopRunInMode + 104
17 GraphicsServices 0x3572c439 GSEventRunModal + 136
18 UIKit 0x3292fe7d UIApplicationMain + 1080
19 MyApp 0x0000321f main (main.m:14)
This crash is likely a result of an error on your part. If you look at few of lines at the top of the stack trace it should give you a hint as to what is wrong.
1 CoreFoundation 0x31cd23fd -[NSObject performSelector:withObject:withObject:] + 52
2 UIKit 0x3291cfaf -[UIApplication sendAction:to:from:forEvent:] + 62
3 UIKit 0x3291cf6b -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 30
4 UIKit 0x3291cf49 -[UIControl sendAction:to:forEvent:] + 44
It looks like somewhere you have an object (possibly a view controller) that is registered for events but is deallocated too soon. The best thing to do is try to reproduce this while attached to the debugger. Start by checking code that handles UIControl callbacks such as UIButtons.
Okay, I have a program that I'm trying to test on my iPad. I have it all set up so that the app is able to get on the iPad just fine, but running it on the iPad is a different story.
Now, the thing is, my program was working fine on the debug configuration, but now it won't work on that, either. It's strange, because before I was able to get to a certain part of the program before it crashed, but now it's crashing before that.
The debugger is hardly helping. When I use the debugger window, it will step into one line of code, and then suddenly jump back to another line of code.
I really don't understand what's going on. The crash is now occuring in a root view controller that I have set up, called 'FunctionMachineViewController. Here's the code for when it crashes:
-(IBAction)startOnePlayer:(id)sender
{
GameViewController* GameView = [[GameViewController alloc] initWithNibName:nil bundle:nil];
[GameView isOnePlayer:YES withLevel:startLevelNumber];
[self presentModalViewController:GameView animated:YES];
}
GameViewController is supposed to initialize just fine, but the debugger shows it as uninitialized. I even tried setting it up in the header file instead, but the problem still happens. Has anyone else come across these problems? Any help would be greatly appreciated!
EDIT: Here's what the console says when it crashes:
[Session started at 2011-06-02 21:46:10 -0700.]
2011-06-02 21:46:13.309 FunctionMachine[5033:207] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<GameViewController 0x4b2a890> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Yminus2.'
*** Call stack at first throw:
(
0 CoreFoundation 0x00dd55a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f29313 objc_exception_throw + 44
2 CoreFoundation 0x00dd54e1 -[NSException raise] + 17
3 Foundation 0x0003d677 _NSSetUsingKeyValueSetter + 135
4 Foundation 0x0003d5e5 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 285
5 UIKit 0x004c130c -[UIRuntimeOutletConnection connect] + 112
6 CoreFoundation 0x00d4b8cf -[NSArray makeObjectsPerformSelector:] + 239
7 UIKit 0x004bfd23 -[UINib instantiateWithOwner:options:] + 1041
8 UIKit 0x004c1ab7 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 168
9 UIKit 0x00377628 -[UIViewController _loadViewFromNibNamed:bundle:] + 70
10 UIKit 0x00375134 -[UIViewController loadView] + 120
11 UIKit 0x0037500e -[UIViewController view] + 56
12 UIKit 0x00376a3d -[UIViewController viewControllerForRotation] + 63
13 UIKit 0x00372988 -[UIViewController _visibleView] + 90
14 UIKit 0x0061493c -[UIClientRotationContext initWithClient:toOrientation:duration:andWindow:] + 354
15 UIKit 0x002ec81e -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 954
16 UIKit 0x00574619 -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:] + 1381
17 UIKit 0x0037965d -[UIViewController presentModalViewController:withTransition:] + 3478
18 FunctionMachine 0x00001fdd -[FunctionMachineViewController startOnePlayer:] + 227
19 UIKit 0x002c54fd -[UIApplication sendAction:to:from:forEvent:] + 119
20 UIKit 0x00355799 -[UIControl sendAction:to:forEvent:] + 67
21 UIKit 0x00357c2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
22 UIKit 0x00356a1c -[UIControl touchesBegan:withEvent:] + 277
23 UIKit 0x002e9d41 -[UIWindow _sendTouchesForEvent:] + 395
24 UIKit 0x002cac37 -[UIApplication sendEvent:] + 447
25 UIKit 0x002cff2e _UIApplicationHandleEvent + 7576
26 GraphicsServices 0x0172d992 PurpleEventCallback + 1550
27 CoreFoundation 0x00db6944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
28 CoreFoundation 0x00d16cf7 __CFRunLoopDoSource1 + 215
29 CoreFoundation 0x00d13f83 __CFRunLoopRun + 979
30 CoreFoundation 0x00d13840 CFRunLoopRunSpecific + 208
31 CoreFoundation 0x00d13761 CFRunLoopRunInMode + 97
32 GraphicsServices 0x0172c1c4 GSEventRunModal + 217
33 GraphicsServices 0x0172c289 GSEventRun + 115
34 UIKit 0x002d3c93 UIApplicationMain + 1160
35 FunctionMachine 0x00001c64 main + 102
36 FunctionMachine 0x00001bf5 start + 53
37 ??? 0x00000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'
Turns out that, when I went and changed the name of several UILabels in the header of GameViewController, I forgot to fix the names in the interface builder, too. Don't know if that was what was causing the debugger to act weird, but everything is working now!
The implementation file looks like this:
#import "MovieViewController.h"
#import "Movie.h"
#import "MovieEditorViewController.h"
#implementation MovieViewController
#synthesize titleLabel;
#synthesize boxOfficeGrossLabel;
#synthesize summaryLabel;
#synthesize movie;
but i'm thinking my problem is not adding #synthesize statements for the MovieEditorViewController header file. If it's not this then I've included the debugging log below.
I'm really new to programming and I really can't seem to figure out whats going wrong. When I run the iOS simulator and click the button to display editable text fields the program terminates and goes back to the homescreen. The debugger shows as follows:
[Session started at 2011-06-04
13:00:00 +0100.] 2011-06-04
13:00:05.568 Movie[6678:207]
-[MovieViewController editingViewController]: unrecognized
selector sent to instance 0x8a3bd10
2011-06-04 13:00:05.572
Movie[6678:207] * Terminating app
due to uncaught exception
'NSInvalidArgumentException', reason:
'-[MovieViewController
editingViewController]: unrecognized
selector sent to instance 0x8a3bd10'
* Call stack at first throw: ( 0 CoreFoundation
0x00dc95a9 exceptionPreprocess + 185
1 libobjc.A.dylib
0x00f1d313 objc_exception_throw + 44
2 CoreFoundation
0x00dcb0bb -[NSObject(NSObject)
doesNotRecognizeSelector:] + 187 3
CoreFoundation
0x00d3a966 __forwarding + 966 4
CoreFoundation
0x00d3a522 _CF_forwarding_prep_0 + 50
5 Movie
0x000021b4 -[MovieViewController edit]
+ 62 6 UIKit 0x002b94fd -[UIApplication
sendAction:to:from:forEvent:] + 119 7
UIKit
0x00349799 -[UIControl
sendAction:to:forEvent:] + 67 8
UIKit
0x0034bc2b -[UIControl(Internal)
_sendActionsForEvents:withEvent:] + 527 9 UIKit
0x0034a7d8 -[UIControl
touchesEnded:withEvent:] + 458 10
UIKit
0x002ddded -[UIWindow
_sendTouchesForEvent:] + 567 11 UIKit
0x002bec37 -[UIApplication sendEvent:]
+ 447 12 UIKit 0x002c3f2e _UIApplicationHandleEvent +
7576 13 GraphicsServices
0x01721992 PurpleEventCallback + 1550
14 CoreFoundation
0x00daa944
CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION
+ 52 15 CoreFoundation 0x00d0acf7 __CFRunLoopDoSource1 + 215
16 CoreFoundation
0x00d07f83 __CFRunLoopRun + 979 17
CoreFoundation
0x00d07840 CFRunLoopRunSpecific + 208
18 CoreFoundation
0x00d07761 CFRunLoopRunInMode + 97 19
GraphicsServices
0x017201c4 GSEventRunModal + 217 20
GraphicsServices
0x01720289 GSEventRun + 115 21 UIKit
0x002c7c93 UIApplicationMain + 1160
22 Movie
0x00001b40 main + 102 23 Movie
0x00001ad1 start + 53 24 ???
0x00000001 0x0 + 1 ) terminate called
after throwing an instance of
'NSException'
If anyone could point me in the right direction i'd be very grateful...
Thanks!
Looks like you're in the iPhone SDK Development book. Have you made it to section 4.8 yet? The error says that your MovieViewController doesn't have a property named editingViewController, which is something you add in 4.8. The project won't build and run until you get through section 4.9, which is where you make some connections.
Can you please help me, my application worked well, but when i did a modification on it, (i don't remember what kind of modification) i got this message in the console:
[Session started at 2010-11-21 17:15:36 -0500.]
2010-11-21 17:15:37.472 ITMFR[3942:207] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UICustomObject 0x6a50d90> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key window.'
*** Call stack at first throw:
(
0 CoreFoundation 0x021f2b99 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x0234240e objc_exception_throw + 47
2 CoreFoundation 0x021f2ad1 -[NSException raise] + 17
3 Foundation 0x000320f3 _NSSetUsingKeyValueSetter + 135
4 Foundation 0x00032061 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 285
5 UIKit 0x004b070a -[UIRuntimeOutletConnection connect] + 112
6 CoreFoundation 0x02168d0f -[NSArray makeObjectsPerformSelector:] + 239
7 UIKit 0x004af121 -[UINib instantiateWithOwner:options:] + 1041
8 UIKit 0x004b0eb5 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 168
9 UIKit 0x002bc402 -[UIApplication _loadMainNibFile] + 172
10 UIKit 0x002bd31c -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 198
11 UIKit 0x002c73ec -[UIApplication handleEvent:withNewEvent:] + 1958
12 UIKit 0x002bfb3c -[UIApplication sendEvent:] + 71
13 UIKit 0x002c49bf _UIApplicationHandleEvent + 7672
14 GraphicsServices 0x02ad2822 PurpleEventCallback + 1550
15 CoreFoundation 0x021d3ff4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
16 CoreFoundation 0x02134807 __CFRunLoopDoSource1 + 215
17 CoreFoundation 0x02131a93 __CFRunLoopRun + 979
18 CoreFoundation 0x02131350 CFRunLoopRunSpecific + 208
19 CoreFoundation 0x02131271 CFRunLoopRunInMode + 97
20 UIKit 0x002bcc6d -[UIApplication _run] + 625
21 UIKit 0x002c8af2 UIApplicationMain + 1160
22 ITMFR 0x00003eb8 main + 102
23 ITMFR 0x000028e9 start + 53
24 ??? 0x00000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'
please, HELP ME!!
From the crash report, I would guess that the change you made was to the nib / xib file in Interface builder, specifically in binding a value to a button / control. Check the Inspector for all of your UI controls to make sure that any Button Bindings that are bound to Shared User Defaults Controller have controller key values, and not something else.
This happened to me because I forgot #synthesize.
#synthesize window;
In my view controller.