NSMutableString stringWithString Crashes the App - ios

This is the code i have written to display content in a label.
UILabel *content;
content = [[UILabel alloc] initWithFrame:CGRectMake(8, loaderView.frame.size.height+loaderView.frame.origin.y+15, screenBounds.size.width-16, 10)];
NSMutableString *articleContent = [NSMutableString stringWithString:[articleDictionary objectForKey:#"content"]];
The app crashes while executing the line stringWithString.I checked lot of posts related to NSMutableString crash. But i am newbie and trying to understand memory allocation.
should i check for content if nil,or is there any memory leak, what am i doing wrong.
I am using xcode 6.3, ios 8.4
the content is NSString and it has value. How to rewrite the code checking for nil
This is the exception i am getting.
0 CoreFoundation 0x0000000183c3cf48 __exceptionPreprocess + 120
1 libobjc.A.dylib 0x00000001986fff80 objc_exception_throw + 52
2 CoreFoundation 0x0000000183c3ce90 +[NSException raise:format:] + 116
3 Foundation 0x0000000184ab9680 -[NSPlaceholderMutableString initWithString:] + 108
4 Foundation 0x0000000184ab9764 +[NSString stringWithString:] + 44
5 MyProject 0x0000000100104da8 -[ArticleDetailPage renderPage] (ArticleDetailPage.m:599)
6 MyProject 0x000000010010469c -[ArticleDetailPage viewWillAppear:] (ArticleDetailPage.m:556)
7 UIKit 0x00000001891e85f4 -[UIViewController _setViewAppearState:isAnimating:] + 624
8 UIKit 0x00000001891e8368 -[UIViewController __viewWillAppear:] + 152
9 UIKit 0x0000000189381fb4 -[UINavigationController _startCustomTransition:] + 1048
10 UIKit 0x000000018928e190 -[UINavigationController _startDeferredTransitionIfNeeded:] + 684
11 UIKit 0x000000018928de6c -[UINavigationController __viewWillLayoutSubviews] + 56
12 UIKit 0x000000018928ddd4 -[UILayoutContainerView layoutSubviews] + 204
13 UIKit 0x00000001891cb7ac -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 640
14 QuartzCore 0x00000001889cab58 -[CALayer layoutSublayers] + 144
15 QuartzCore 0x00000001889c5764 CA::Layer::layout_if_needed() + 288
16 QuartzCore 0x00000001889c5624 CA::Layer::layout_and_display_if_needed() + 28
17 QuartzCore 0x00000001889c4cc0 CA::Context::commit_transaction() + 248
18 QuartzCore 0x00000001889c4a08 CA::Transaction::commit() + 508
19 UIKit 0x00000001891c19d8 _afterCACommitHandler + 176
20 CoreFoundation 0x0000000183bf3bd0 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 28
21 CoreFoundation 0x0000000183bf1974 __CFRunLoopDoObservers + 368
22 CoreFoundation 0x0000000183bf1da4 __CFRunLoopRun + 924
23 CoreFoundation 0x0000000183b20ca0 CFRunLoopRunSpecific + 380
24 GraphicsServices 0x000000018ed5c088 GSEventRunModal + 176
25 UIKit 0x0000000189238ffc UIApplicationMain + 200
26 MyProject 0x00000001000ee7b4 main (main.m:15)
27 libdyld.dylib 0x0000000198f4e8b8 start + 0

Try this instead:
NSMutableString *articleContent = [articleDictionary[#"content"] mutableCopy];
If it's not set in the dictionary it will be nil and it avoids the potential crash in [NSMutableString stringWithString] (calling mutableCopy on nil is silently ignored).

Related

FMDatabaseQueue crash

I'm using FMDB in my app. I'm seeing this random crash happening during the app launch.
-(void) addActivities{
[[[DB sharedManager]getSecureQueue] inTransaction:^(FMDatabase * _Nonnull db, BOOL * _Nonnull rollback) {
//process data
}];
}
getSecureQueue returns a FMDatabaseQueue:
_secureQueue= [FMDatabaseQueue databaseQueueWithPath:dbPath];
I checked that _SecureQueue is not nil.
The crash log:
Crashed: fmdb.<FMDatabaseQueue: 0x170242610>
0 libdispatch.dylib 0x18ad83634 _dispatch_barrier_sync_f_slow + 560
1 FMDB 0x101040bd8 -[FMDatabaseQueue beginTransaction:withBlock:] (FMDatabaseQueue.m:228)
2 FMDB 0x101040bd8 -[FMDatabaseQueue beginTransaction:withBlock:] (FMDatabaseQueue.m:228)
3 iPhoneHandheldACT 0x1001e4870 -[HHCTabBarController addActivities] (HHCTabBarController.m:145)
4 iPhoneHandheldACT 0x1001e553c __49-[HHCTabBarController migrateIntoRealm]_block_invoke (HHCTabBarController.m:226)
5 FMDB 0x101040ac4 __30-[FMDatabaseQueue inDatabase:]_block_invoke (FMDatabaseQueue.m:188)
6 libdispatch.dylib 0x18ad729a0 _dispatch_client_callout + 16
7 libdispatch.dylib 0x18ad7fee0 _dispatch_barrier_sync_f_invoke + 84
8 FMDB 0x101040a34 -[FMDatabaseQueue inDatabase:] (FMDatabaseQueue.m:202)
9 iPhoneHandheldACT 0x1001e4ce8 -[HHCTabBarController migrateIntoRealm] (HHCTabBarController.m:181)
10 iPhoneHandheldACT 0x1001e4270 -[HHCTabBarController viewDidLoad] (HHCTabBarController.m:77)
11 UIKit 0x191fecb04 -[UIViewController loadViewIfRequired] + 1036
12 UIKit 0x192004590 -[UIViewController __viewWillAppear:] + 132
13 UIKit 0x192188e18 -[UINavigationController _startCustomTransition:] + 1144
14 UIKit 0x1920a37bc -[UINavigationController _startDeferredTransitionIfNeeded:] + 676
15 UIKit 0x1920a3424 -[UINavigationController __viewWillLayoutSubviews] + 64
16 UIKit 0x1920a3388 -[UILayoutContainerView layoutSubviews] + 188
17 UIKit 0x191fe9cc0 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1200
18 QuartzCore 0x18f1da274 -[CALayer layoutSublayers] + 148
19 QuartzCore 0x18f1cede8 CA::Layer::layout_if_needed(CA::Transaction*) + 292
20 QuartzCore 0x18f1ceca8 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 32
21 QuartzCore 0x18f14a34c CA::Context::commit_transaction(CA::Transaction*) + 252
22 QuartzCore 0x18f1713ac CA::Transaction::commit() + 504
23 QuartzCore 0x18f171e78 CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 120
24 CoreFoundation 0x18be689a8 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
25 CoreFoundation 0x18be66630 __CFRunLoopDoObservers + 372
26 CoreFoundation 0x18bd96dc4 CFRunLoopRunSpecific + 456
27 UIFoundation 0x191f95134 -[NSHTMLReader _loadUsingWebKit] + 1764
28 Foundation 0x18c9af50c __NSThreadPerformPerform + 340
29 CoreFoundation 0x18be6942c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
30 CoreFoundation 0x18be68d9c __CFRunLoopDoSources0 + 540
31 CoreFoundation 0x18be669a8 __CFRunLoopRun + 744
32 CoreFoundation 0x18bd96da4 CFRunLoopRunSpecific + 424
33 GraphicsServices 0x18d801074 GSEventRunModal + 100
34 UIKit 0x192051c9c UIApplicationMain + 208
35 iPhoneHandheldACT 0x1000a7bf4 main (main.m:16)
36 libdyld.dylib 0x18ada559c start + 4
On line 8, it looks like you're already inside an inDatabase call (i.e. you're already in FMDatabaseQueue when you try to enter the queue again). Don't try to enter serial queue when you're already inside that serial queue. At best, you'll deadlock. If you do that at the wrong time (e.g. during app startup), the watchdog process will kill your app with exception code 0x8badf00d ("ate bad food"; lol; see Technical Note TN2151: Understanding and Analyzing Application Crash Reports).

Getting [__NSArrayM insertObject:atIndex:] tough the value is not nil

I am working on UIPageViewController and I make strong for the global instance in properties below code:
UIViewController* aController = someviewController;//(I am getting the aController value)
NSArray* viewControllers = [[NSArray alloc] initWithObjects:aController, nil];
//(I am getting the viewControllers value as well).
[self.pageController setViewControllers:viewControllers
direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
//(I am getting crash here saying [__NSArrayM insertObject:atIndex:]: object cannot be nil').
All the instances are holding the values don't know where viewControllers is becoming nil.
please let me know
0 CoreFoundation 0x0000000105471e65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010626ddeb objc_exception_throw + 48
2 CoreFoundation 0x00000001053388c5 -[__NSArrayM insertObject:atIndex:] + 901
3 0x0000000103df0b5e -[MZDashBoardViewController viewDidLoad] + 1470
4 UIKit 0x00000001071fbf98 -[UIViewController loadViewIfRequired] + 1198
5 UIKit 0x00000001071fc2e7 -[UIViewController view] + 27
6 UIKit 0x00000001077d57df -[UIPageViewController _setViewControllers:withScrollInDirection:animated:completion:] + 585
7 UIKit 0x00000001077d59fb -[UIPageViewController setViewControllers:direction:animated:completion:] + 166
8 0x0000000103decba5 -[MZMainDashViewController viewDidLoad] + 2997
9 UIKit 0x00000001071fbf98 -[UIViewController loadViewIfRequired] + 1198
10 UIKit 0x0000000107240651 -[UINavigationController _layoutViewController:] + 54
11 UIKit 0x0000000107240f26 -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 462
12 UIKit 0x0000000107241098 -[UINavigationController _startTransition:fromViewController:toViewController:] + 126
13 UIKit 0x00000001072422f1 -[UINavigationController _startDeferredTransitionIfNeeded:] + 890
14 UIKit 0x00000001072433af -[UINavigationController __viewWillLayoutSubviews] + 57
15 UIKit 0x00000001073e9ff7 -[UILayoutContainerView layoutSubviews] + 248
16 UIKit 0x000000010711c4a3 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703
17 QuartzCore 0x0000000106d8359a -[CALayer layoutSublayers] + 146
18 QuartzCore 0x0000000106d77e70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
19 QuartzCore 0x0000000106d77cee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
20 QuartzCore 0x0000000106d6c475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
21 QuartzCore 0x0000000106d99c0a _ZN2CA11Transaction6commitEv + 486
22 QuartzCore 0x0000000106d9a37c _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
23 CoreFoundation 0x000000010539d367 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
24 CoreFoundation 0x000000010539d2d7 __CFRunLoopDoObservers + 391
25 CoreFoundation 0x0000000105392f2b __CFRunLoopRun + 1147
26 CoreFoundation 0x0000000105392828 CFRunLoopRunSpecific + 488
27 GraphicsServices 0x000000010988fad2 GSEventRunModal + 161
28 UIKit 0x0000000107065610 UIApplicationMain + 171
29 0x0000000103dd2d3f main + 111
30 libdyld.dylib 0x00000001086e092d start + 1
31 ??? 0x0000000000000001 0x0 + 1
Clearly you are trying to insert an object into an immutable array, not a mutable array. Somewhere inside viewDidLoad. Remember just because a variable has type NSMutableArray* doesn't mean it is.

iOS app odd crashes [UINavigationController _startCustomTransition:]

I have my app in alpha test, and using fabric to capture the crash logs. I had encountered a crash, and I have the below trace but have no idea what is going on. I tried to reproduce it with Xcode, but hardly can reproduce.
Could any one help on this? or share some ideas?
Thread : Crashed: com.apple.main-thread
0 libsystem_platform.dylib 0x0000000197c7d8d4 OSAtomicCompareAndSwap32Barrier + 12
1 libobjc.A.dylib 0x0000000197468120 realizeClass(objc_class*) + 100
2 libobjc.A.dylib 0x000000019746c930 lookUpImpOrForward + 224
3 libobjc.A.dylib 0x0000000197477db8 _objc_msgSend_uncached_impcache + 56
4 UIKit 0x000000018b66bef4 -[UINavigationController _startCustomTransition:] + 972
5 UIKit 0x000000018b57d630 -[UINavigationController _startDeferredTransitionIfNeeded:] + 468
6 UIKit 0x000000018b57d3fc -[UINavigationController __viewWillLayoutSubviews] + 56
7 UIKit 0x000000018b57d37c -[UILayoutContainerView layoutSubviews] + 200
8 UIKit 0x000000018b4c5d2c -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 572
9 QuartzCore 0x000000018ae1d994 -[CALayer layoutSublayers] + 168
10 QuartzCore 0x000000018ae18564 CA::Layer::layout_if_needed(CA::Transaction*) + 320
11 QuartzCore 0x000000018ae18408 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 32
12 QuartzCore 0x000000018ae17c08 CA::Context::commit_transaction(CA::Transaction*) + 276
13 QuartzCore 0x000000018ae1798c CA::Transaction::commit() + 436
14 UIKit 0x000000018b4c7c48 _UIApplicationHandleEventQueue + 1700
15 CoreFoundation 0x0000000186cd29ec __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
16 CoreFoundation 0x0000000186cd1c90 __CFRunLoopDoSources0 + 264
17 CoreFoundation 0x0000000186ccfd40 __CFRunLoopRun + 712
18 CoreFoundation 0x0000000186bfd0a4 CFRunLoopRunSpecific + 396
19 GraphicsServices 0x000000018fd975a4 GSEventRunModal + 168
20 UIKit 0x000000018b52eaa4 UIApplicationMain + 1488
21 MCompass 0x00000001001631c8 main (main.m:16)
22 libdyld.dylib 0x0000000197ad2a08 start + 4
It is hard to tell without seeing some more details, but maybe this can help: http://aplus.rs/2013/beware-a-crashing-bug-lurks-with-uinavigationcontroller-transitions/

App crashing with error: -[NSNull length]: unrecognized selector sent to instance

This line [self.label setFont:[MyUtilityClass fontWithSize:13.0f]]; of code is giving me the -[NSNull length]: crash. label as many of you guys can guess is a UILabel. The only logical explanation I can think of is UILabel text is null. I will post my utility class below just in case it helps.
MyUtilityClass
+ (UIFont *) fontWithSize : (CGFloat) size
{
UIFont *font = [UIFont fontWithName:#"Roboto-Regular" size:size];
return font;
}
edit: This is the error message I've received. I can copy and paste the raw data if you need me to. This is a crash a user received according to crashlytics.
Fatal Exception: NSInvalidArgumentException
-[NSNull length]: unrecognized selector sent to instance 0x378e23f0
This is the raw data of the crash.
Thread : Fatal Exception: NSInvalidArgumentException
0 CoreFoundation 0x297c3c1f __exceptionPreprocess + 126
1 libobjc.A.dylib 0x36f97c8b objc_exception_throw + 38
2 CoreFoundation 0x297c9039 __methodDescriptionForSelector
3 CoreFoundation 0x297c6f57 ___forwarding___ + 714
4 CoreFoundation 0x296f8df8 _CF_forwarding_prep_0 + 24
5 UIKit 0x2cc8b1b5 -[UILabel _setFont:] + 100
6 MyApp 0x00137c29 -[MyViewController initUI] (MyViewController.m:90)
7 MyApp 0x0013765b -[MyViewController viewDidLoad] (MyViewController.m:51)
8 UIKit 0x2cc82f8f -[UIViewController loadViewIfRequired] + 602
9 UIKit 0x2cd2cd95 -[UINavigationController _layoutViewController:] + 32
10 UIKit 0x2cd2ccbd -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 228
11 UIKit 0x2cd2c253 -[UINavigationController _startTransition:fromViewController:toViewController:] + 74
12 UIKit 0x2cd2bf83 -[UINavigationController _startDeferredTransitionIfNeeded:] + 578
13 UIKit 0x2cd2bced -[UINavigationController __viewWillLayoutSubviews] + 44
14 UIKit 0x2cd2bc81 -[UILayoutContainerView layoutSubviews] + 184
15 UIKit 0x2cc804d7 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 514
16 QuartzCore 0x2c6a8a0d -[CALayer layoutSublayers] + 136
17 QuartzCore 0x2c6a43e5 CA::Layer::layout_if_needed(CA::Transaction*) + 360
18 QuartzCore 0x2c6a426d CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 16
19 QuartzCore 0x2c6a3c51 CA::Context::commit_transaction(CA::Transaction*) + 224
20 QuartzCore 0x2c6a3a55 CA::Transaction::commit() + 324
21 UIKit 0x2cc78965 _afterCACommitHandler + 132
22 CoreFoundation 0x2978a3b5 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 20
23 CoreFoundation 0x29787a73 __CFRunLoopDoObservers + 278
24 CoreFoundation 0x29787e7b __CFRunLoopRun + 914
25 CoreFoundation 0x296d6211 CFRunLoopRunSpecific + 476
26 CoreFoundation 0x296d6023 CFRunLoopRunInMode + 106
27 GraphicsServices 0x30a8f0a9 GSEventRunModal + 136
28 UIKit 0x2cce21d1 UIApplicationMain + 1440
29 MyApp 0x000a07e7 main (main.m:16)
30 libdyld.dylib 0x37517aaf start + 2
It's very likely that you are trying to get the length of what you think is an NSString but is actually NSNull.
I got this issue with my code & resolved it by using system fonts. The case was it couldn't load the font which I had specified into the code. Try & let me know if it help.
Happy Coding, cheers,
Ratneshwar

ios fatal exception error - NSInvalidArgumentException NSInvalidArgumentException

I got crash issue from Crashlytics.
UIKit
-[UIView(Internal) _addSubview:positioned:relativeTo:]
Fatal Exception: NSInvalidArgumentException NSInvalidArgumentException
Can't add self as subview
Thread : Fatal Exception: NSInvalidArgumentException
0 CoreFoundation 0x2e9a0f4b __exceptionPreprocess + 130
1 libobjc.A.dylib 0x391376af objc_exception_throw + 38
2 CoreFoundation 0x2e9a0e8d -[NSException initWithCoder:]
3 UIKit 0x3111f48f -[UIView(Internal) _addSubview:positioned:relativeTo:] + 110
4 UIKit 0x3111f417 -[UIView(Hierarchy) addSubview:] + 30
5 UIKit 0x312e7c2f __53-[_UINavigationParallaxTransition animateTransition:]_block_invoke + 1402
6 UIKit 0x311256e5 +[UIView(Animation) performWithoutAnimation:] + 72
7 UIKit 0x312e7479 -[_UINavigationParallaxTransition animateTransition:] + 728
8 UIKit 0x312a4ba7 -[UINavigationController _startCustomTransition:] + 2614
9 UIKit 0x311c2273 -[UINavigationController _startDeferredTransitionIfNeeded:] + 418
10 UIKit 0x311c207d -[UINavigationController __viewWillLayoutSubviews] + 44
11 UIKit 0x311c2015 -[UILayoutContainerView layoutSubviews] + 184
12 UIKit 0x31113da3 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 346
13 QuartzCore 0x30d9ac6b -[CALayer layoutSublayers] + 142
14 QuartzCore 0x30d9647b CA::Layer::layout_if_needed(CA::Transaction*) + 350
15 QuartzCore 0x30d9630d CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 16
16 QuartzCore 0x30d95d1f CA::Context::commit_transaction(CA::Transaction*) + 230
17 QuartzCore 0x30d95b2f CA::Transaction::commit() + 314
18 QuartzCore 0x30d8f85d CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 56
19 CoreFoundation 0x2e96c1cd
20 CoreFoundation 0x2e969b71 __CFRunLoopDoObservers + 284
21 CoreFoundation 0x2e969eb3 __CFRunLoopRun + 730
22 CoreFoundation 0x2e8d4c27 CFRunLoopRunSpecific + 522
23 CoreFoundation 0x2e8d4a0b CFRunLoopRunInMode + 106
24 GraphicsServices 0x335c8283 GSEventRunModal + 138
25 UIKit 0x31178049 UIApplicationMain + 1136
26 Tapastic 0x000764ef main (main.m:16)
27 Tapastic 0x00075bf0 start
I cannot find where this crash happened in codes. So how can I find where this crash is and fix it? Please let me know. Thanks in advance.

Resources