Low memory warning causes a crash on iOS5 - unloadViewForced - ios

I have been able to replicate a low-memory crash on iOS devices that are running iOS5. Every time the stack trace points to the same place, so I know exactly where the problem is. Unfortunately I don't know how to fix it. As you can see below, the crash occurs when CatViewController receives a memory warning. At that point its view gets forcibly unloaded, then the crash happens:
0 libobjc.A.dylib 0x37174f7e objc_msgSend + 22
1 UIKit 0x317eab66 -[UIViewController unloadViewForced:] + 130
2 UIKit 0x31932492 -[UIViewController purgeMemoryForReason:] + 58
3 MyApp 0x001353c0 -[CatViewController didReceiveMemoryWarning] (CatViewController.m:89)
4 Foundation 0x339e34f8 __57-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke_0 + 12
5 CoreFoundation 0x35178540 ___CFXNotificationPost_block_invoke_0 + 64
6 CoreFoundation 0x35104090 _CFXNotificationPost + 1400
7 Foundation 0x339573e4 -[NSNotificationCenter postNotificationName:object:userInfo:] + 60
8 Foundation 0x33958c14 -[NSNotificationCenter postNotificationName:object:] + 24
9 UIKit 0x318fd26a -[UIApplication _performMemoryWarning] + 74
10 UIKit 0x318fd364 -[UIApplication _receivedMemoryNotification] + 168
11 libdispatch.dylib 0x37ae6252 _dispatch_source_invoke + 510
12 libdispatch.dylib 0x37ae3b1e _dispatch_queue_invoke$VARIANT$up + 42
13 libdispatch.dylib 0x37ae3e64 _dispatch_main_queue_callback_4CF$VARIANT$up + 152
14 CoreFoundation 0x3517f2a6 __CFRunLoopRun + 1262
15 CoreFoundation 0x3510249e CFRunLoopRunSpecific + 294
16 CoreFoundation 0x35102366 CFRunLoopRunInMode + 98
17 GraphicsServices 0x3476d432 GSEventRunModal + 130
18 UIKit 0x31779e76 UIApplicationMain + 1074
19 MyApp 0x00087142 main (main.m:16)
20 MyApp 0x000870c8 start + 32
So I believe this has something to do with the way the view is getting unloaded on iOS5. This view controller is not removing itself from any notifications, so this solution doesn't work. Line 89 of CatViewController is just this:
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; //this is line 89
}
And viewDidUnload just looks like this:
- (void)viewDidUnload {
[self setBackgroundView:nil];
[self setContentView:nil];
[self setSomeScrollView:nil];
[super viewDidUnload];
}
Any ideas on how I can handle this gracefully without crashing?
EDIT: Here's more info on another crash report:
0 CoreFoundation 0x351ac88f __exceptionPreprocess + 163
1 libobjc.A.dylib 0x3717a259 objc_exception_throw + 33
2 CoreFoundation 0x351afa9b -[NSObject doesNotRecognizeSelector:] + 175
3 CoreFoundation 0x351ae915 ___forwarding___ + 301
4 CoreFoundation 0x35109650 _CF_forwarding_prep_0 + 48
5 UIKit 0x317eabdf -[UIViewController unloadViewForced:] + 251
6 UIKit 0x31932499 -[UIViewController purgeMemoryForReason:] + 65
7 MyApp 0x000d541b -[CatViewController didReceiveMemoryWarning] (CatViewController.m:96)

Your app is not crashing because it is out of memory. In your crash report, you can see that the exception thrown was actually [NSObject doesNotRecognizeSelector:].
Firstly, you should move those set to nil statements to didReceiveMemoryWarning; this is the preferred method since viewDidUnload is no longer supported in iOS 6.
Secondly, to find your crash, step through those statements where you set your properties to nil and make sure none of those cause the crash. Note that you will only have methods with those signatures (self set<propertyName>:]) if those variables are declared as properties, not as mere instance variables.

Related

Objective-C iOS app crashes during CFStringAppend for unknown reason

In my iPhone app, for some users the app will always crash, whenever they open a certain window. The same window works fine for me and other people, but always causes a crash for some people. It is unclear at this moment what the difference is between people for whom it fails, and for whom it works fine.
The animation to open the specific view will run fine, but will then cause a crash immediately after the animation is finished.
They have sent me the crashlog, but it is hard to understand what is exactly going wrong. I am hoping you can help me understand. The cause for the failure as given by the crashlog is the following exception:
Last Exception Backtrace:
0 CoreFoundation 0x1836ffd38 __exceptionPreprocess + 124
1 libobjc.A.dylib 0x182c14528 objc_exception_throw + 55
2 CoreFoundation 0x18370d1f8 -[NSObject+ 1372664 (NSObject) doesNotRecognizeSelector:] + 139
3 UIKit 0x18cec7cc4 -[UIResponder doesNotRecognizeSelector:] + 295
4 CoreFoundation 0x1837056e4 ___forwarding___ + 1379
5 CoreFoundation 0x1835eb0dc _CF_forwarding_prep_0 + 91
6 CoreFoundation 0x1835d6be8 CFStringAppend + 519
7 CoreFoundation 0x1836bddf0 __CFStringAppendFormatCore + 9271
8 CoreFoundation 0x1836bf658 _CFStringCreateWithFormatAndArgumentsAux2 + 131
9 AccessibilityUtilities 0x192d6b388 _AXStringForArgs + 279
10 UIKit 0x1a242adf8 -[UIViewControllerAccessibility viewDidAppear:] + 267
11 UIKit 0x18cb2869c -[UIViewController _setViewAppearState:isAnimating:] + 851
12 UIKit 0x18cb28c08 -[UIViewController _endAppearanceTransition:] + 227
13 UIKit 0x18cbcee00 -[UINavigationController navigationTransitionView:didEndTransition:fromView:toView:] + 1327
14 UIKit 0x1a2440cd4 -[UINavigationControllerAccessibility navigationTransitionView:didEndTransition:fromView:toView:] + 111
15 UIKit 0x18cc96bbc __49-[UINavigationController _startCustomTransition:]_block_invoke + 251
16 UIKit 0x18cc229d8 -[_UIViewControllerTransitionContext completeTransition:] + 115
17 UIKit 0x18cd67d30 __53-[_UINavigationParallaxTransition animateTransition:]_block_invoke.124 + 751
18 UIKit 0x18cb47d7c -[UIViewAnimationBlockDelegate _didEndBlockAnimation:finished:context:] + 763
19 UIKit 0x18cb4770c -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 311
20 UIKit 0x18cb47418 -[UIViewAnimationState animationDidStop:finished:] + 295
21 UIKit 0x1a2468970 -[UIViewAnimationStateAccessibility animationDidStop:finished:] + 131
22 QuartzCore 0x1876ebd6c CA::Layer::run_animation_callbacks+ 1232236 (void*) + 283
23 libdispatch.dylib 0x183085048 _dispatch_client_callout + 15
24 libdispatch.dylib 0x183091b74 _dispatch_main_queue_callback_4CF$VARIANT$mp + 1015
25 CoreFoundation 0x1836a7f20 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 11
26 CoreFoundation 0x1836a5afc __CFRunLoopRun + 2011
27 CoreFoundation 0x1835c62d8 CFRunLoopRunSpecific + 435
28 GraphicsServices 0x185457f84 GSEventRunModal + 99
29 UIKit 0x18cb73880 UIApplicationMain + 207
30 Flyskyhy 0x10490bd80 main + 32128 (main.m:17)
31 libdyld.dylib 0x1830ea56c start + 3
The crashlog seems to indicate that it goes wrong while doing a CFStringAppend, but it is not clear which string is the problem, or what is wrong with it, or even why a CFStringAppend is needed. All strings visible in the view have been filled in already before the animation starts, and they all are correct.
EDIT:
As requested, here is the code that starts the view. Everything is under a NavigationController, so the new view controller is pushed on the navigation stack to open it.
WaypointEditController *controller = [[WaypointEditController alloc] initWithNibName:#"WayPointEdit" bundle:nil];
controller.navigationItem.title = #"New Waypoint";
// initialisation of other, custom, fields of controller
[self.navigationController pushViewController:controller animated:YES];
The WaypointEditController class that is pushed is derived from UIViewController. The viewWillAppear is overridden, to do initialisation of the fields of the view. But - relevant here - viewDidAppear is not overridden.
In case relevant, here are the most important actions in the viewWillAppear method:
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
self.navigationItem.title = #"New Waypoint";
// other initialisation of internal fields
}
Like you, I have customers reporting this problem and I could see the stack traces that XCode downloaded from production but I could not reproduce it... until I tried enabling "voice over".
The other accessibility options that I tried worked OK.
The stack trace shows that iOS is sending a selector of "length" to an instance of UILabel. This seemed odd. To see what would happen, I added a "length" method to UILabel to return the length of the UILabel's text property. Then it just failed with an unknown selector of _encodingCantBeStoredInEightBitCFString. So clearly iOS thought that my UILabel was something that it wasn't.
This ended up being because "description" was used for the name of my UILabel property/IBOutlet.
To fix the problem I renamed the property from "description" to something different. I suspect that my synthesized "description" getter was overriding NSObject's description method (which returns an NSString which fits the selectors which were being sent to my UILabel).

weird SIGSEGV crash in iOS

About 10+ crash reported by crittercism.
Name: SIGSEGV
Reason: SEGV_MAPERR
Seems it is a UIScrollView/UITableView animating or scrolling issue.
How can I find the root cause?
Stack:
0
libobjc.A.dylib 0x0000000195fafbd0 objc_msgSend + 12
1
UIKit 0x00000001892b1db8 -[UIScrollView(UIScrollViewInternal) _delegateScrollViewAnimationEnded] + 64
2
UIKit 0x00000001892b1d30 -[UIScrollView(UIScrollViewInternal) _scrollViewAnimationEnded:finished:] + 204
3
UIKit 0x0000000189366814 -[UIAnimator stopAnimation:] + 500
4
UIKit 0x0000000189366248 -[UIAnimator(Static) _advanceAnimationsOfType:withTimestamp:] + 332
5
QuartzCore 0x0000000188b4629c CA::Display::DisplayLinkItem::dispatch() + 28
6
QuartzCore 0x0000000188b46134 CA::Display::DisplayLink::dispatch_items() + 320
7
IOKit 0x0000000185921470 IODispatchCalloutFromCFMessage + 372
8
CoreFoundation 0x0000000184712dc4 __CFMachPortPerform + 176
9
CoreFoundation 0x0000000184727a54 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
10
CoreFoundation 0x00000001847279b4 __CFRunLoopDoSource1 + 432
11
CoreFoundation 0x0000000184725934 __CFRunLoopRun + 1636
12
CoreFoundation 0x00000001846512d4 CFRunLoopRunSpecific + 392
13
GraphicsServices 0x000000018de676fc GSEventRunModal + 164
14
UIKit 0x0000000189216fac UIApplicationMain + 1484
15
MyApp 0x000000010001d280 main (main.m:14)
16
libdyld.dylib 0x000000019661aa08 start + 0
I've seen this happen when a controller and/or its table is deallocated during an animation. The OS still triggers delegate methods (e.g., -scrollViewDidEndScrollingAnimation: or -scrollViewDidEndDecelerating:) when it finishes with the animation, but because the objects no longer exist things get weird.
The best fix is to nil the delegate in the controller's dealloc method.

UIButton _setTitleFrozen crash

I have weird crash related to UIButton class. I have not found anything similar on SO yet. The log I get comes from Crashlytics so I also don't know which part of code is causing the issue. I put crash log below, may be some of you have already found out what is creating the problem.
EXC_BAD_ACCESS KERN_INVALID_ADDRESS at 0xd1712d18
0 libobjc.A.dylib objc_msgSend + 5 _setTitleFrozen:
1 UIKit 32-[UIButton _beginTitleAnimation]_block_invoke853 + 28
2 UIKit -[UIViewAnimationBlockDelegate_didEndBlockAnimation:finished:context:] + 308
3 UIKit -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 184
4 UIKit -[UIViewAnimationState animationDidStop:finished:] + 70
5 QuartzCore CA::Layer::run_animation_callbacks(void*) + 234
6 libdispatch.dylib _dispatch_client_callout + 22
7 libdispatch.dylib _dispatch_main_queue_callback_4CF$VARIANT$mp + 712
8 CoreFoundation __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE_ + 8
9 CoreFoundation __CFRunLoopRun + 1512
10 CoreFoundation CFRunLoopRunSpecific + 476
11 CoreFoundation CFRunLoopRunInMode + 106
12 GraphicsServices GSEventRunModal + 136
13 UIKit UIApplicationMain + 1440
Changing the button type from System to Custom resolved this crash for me.
In my case this crash occurred because I was changing the button title right before removing it from the view hierarchy (by dismissing its parent view's VC) while the title change animation was still running.

Crash UIScrollView EXC_BAD_ACCESS

I'm having trouble to diagnostic a crash with UIScrollView
UIKit
-[UIScrollView setContentOffset:]
Exception Type: EXC_BAD_ACCESS Code KERN_INVALID_ADDRESS at 0xe0000008
com.apple.main-thread Crashed
0 libobjc.A.dylib objc_msgSend + 15
1 UIKit -[UIScrollView setContentOffset:] + 618
2 UIKit -[UITableView setContentOffset:] + 330
3 UIKit -[UIScrollView(Static) _adjustContentOffsetIfNecessary] + 1474
4 UIKit -[UIScrollView(UIScrollViewInternal) _stopScrollingNotify:pin:tramplingDragFlags:] + 414
5 UIKit -[UIScrollView(UIScrollViewInternal) _stopScrollingNotify:pin:] + 30
6 UIKit -[UIScrollView removeFromSuperview] + 30
7 ... UIKit -[UIView dealloc] + 296
8 UIKit -[UIView dealloc] + 296
9 libobjc.A.dylib (anonymous namespace)::AutoreleasePoolPage::pop(void*) + 168
10 CoreFoundation _CFAutoreleasePoolPop + 16
11 CoreFoundation __CFRunLoopRun + 1296
12 CoreFoundation CFRunLoopRunSpecific + 356
13 CoreFoundation CFRunLoopRunInMode + 104
14 GraphicsServices GSEventRunModal + 74
15 UIKit UIApplicationMain + 1120
16 iTV Shows main.m line 12
I had the same problem. Setting the TableView and ScrollView delegates (or any other delegates that you might have) to nil in the dealloc method solved the problem.
- (void)dealloc {
self.tableView.delegate = nil;
self.tableView.dataSource = nil;
}
Its hard to give a concrete answer without seeing your code, but the iOS Debugging Magic technical note is always a great resource with tips for debugging crashes.
Technical Note TN2239: iOS Debugging Magic

MapKit exception: Cannot remove an observer for the key path "title"

I'm getting quite a lot of crashes like this.
In my code, I'm not doing any KVO on my MKAnnotation objects that could explain this exception. So to me, it looks like an iOS bug.
Did anybody else experience similar crashes?
*** Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer for the key path "title" from because it is not registered as an observer.'
Thread 0 Crashed:
0 libSystem.B.dylib 0x000792d4 __kill + 8
1 libSystem.B.dylib 0x000792bd raise + 17
2 WhereTo 0x000a430d uncaught_exception_handler (PLCrashReporter.m:137)
3 CoreFoundation 0x000a0adf __handleUncaughtException + 239
4 libobjc.A.dylib 0x00006593 _objc_terminate + 103
5 libstdc++.6.dylib 0x00042df9 __cxxabiv1::__terminate(void (*)()) + 53
6 libstdc++.6.dylib 0x00042e4d std::terminate() + 17
7 libstdc++.6.dylib 0x00042f1d __cxa_throw + 85
8 libobjc.A.dylib 0x000054cb objc_exception_throw + 71
9 CoreFoundation 0x000a07c9 +[NSException raise:format:arguments:] + 69
10 CoreFoundation 0x000a0803 +[NSException raise:format:] + 35
11 Foundation 0x00031b4d -[NSObject(NSKeyValueObserverRegistration) _removeObserver:forProperty:] + 545
12 Foundation 0x000318a1 -[NSObject(NSKeyValueObserverRegistration) removeObserver:forKeyPath:] + 121
13 MapKit 0x00024e75 -[MKAnnotationContainerView _unregisterObserverForBubbleAnnotation:] + 133
14 MapKit 0x0001fd29 -[MKAnnotationContainerView setBubbleAnnotationView:] + 73
15 MapKit 0x0001f4e3 -[MKAnnotationContainerView _showBubbleForAnnotationView:bounce:scrollToFit:userInitiated:avoid:] + 211
16 MapKit 0x0001f235 -[MKAnnotationContainerView _setSelectedAnnotationView:bounce:pressed:scrollToFit:userInitiated:avoid:] + 357
17 MapKit 0x0001f0bf -[MKAnnotationContainerView _setSelectedAnnotationView:bounce:pressed:scrollToFit:userInitiated:] + 83
18 MapKit 0x000248ab -[MKMapView handleTap:] + 195
19 CoreFoundation 0x0003ebbf -[NSObject(NSObject) performSelector:withObject:] + 23
20 UIKit 0x0009a3d3 -[UIGestureRecognizer _updateGestureWithEvent:] + 575
21 UIKit 0x0009a18b -[UIGestureRecognizer _delayedUpdateGesture] + 23
22 UIKit 0x000017b1 _UIGestureRecognizerUpdateObserver + 437
23 CoreFoundation 0x00030c59 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 17
24 CoreFoundation 0x00030acd __CFRunLoopDoObservers + 413
25 CoreFoundation 0x000280cb __CFRunLoopRun + 855
26 CoreFoundation 0x00027c87 CFRunLoopRunSpecific + 231
27 CoreFoundation 0x00027b8f CFRunLoopRunInMode + 59
28 GraphicsServices 0x000044ab GSEventRunModal + 115
29 GraphicsServices 0x00004557 GSEventRun + 63
30 UIKit 0x00037329 -[UIApplication _run] + 413
31 UIKit 0x00034e93 UIApplicationMain + 671
32 WhereTo 0x00003475 main (main.m:14)
I had a similar crash with MapKit starting in OS 4.x.
By 'similar' I mean it was related to KVO and observers but I don't remember the details.
The problem was:
I had a class implenting the MKAnnotation protocol.
But in this class I was accessing the coordinate property without using KVO (directly via the corresponding ivar):
_coordinate = ....
instead of
self.coordinate = ...
It seems that Mapkit is now (starting in OS 4) relying on KVO notification to monitor annotations position.
Turns out, I wrote to the property from background threads which is a no-go. If I make sure to only set it from the main thread, the problem seems to go away.
For future reference; I got the exact same error, but in my case it was caused by trying to programmatically select an annotation that hadn't yet been added to the mapview.
I had a similar issue, but the property getter was a defined function but there was no setter. The MKAnnotation protocol requires a property (getter and setter).
I use same in two MKMapView's and exception thrown when call removeAnnotations: from second map, this try-catch fix problem.
#implementation MyAnnotation //<MKAnnotation>
- (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath
{
#try
{
[super removeObserver:observer forKeyPath:keyPath];
}
#catch(...)
{
}
}
#end

Resources