Here is my first attempt to solve crash issue in iOS app. Sharing some crash logs here.
Exception Type: SIGSEGV
Exception Codes: SEGV_ACCERR at 0x97ad6beb8
Crashed Thread: 11
Application Specific Information:
objc_msgSend() selector name: length
and here is my 11th thread.
Thread 11 Crashed:
0 libobjc.A.dylib 0x0000000180558150 objc_msgSend + 16
1 Foundation 0x00000001824f3f60 -[NSString compare:] + 28
2 Foundation 0x00000001824d88e0 _NSCompareObject + 60
3 CoreFoundation 0x0000000181a99bd0 __CFSimpleMergeSort + 88
4 CoreFoundation 0x0000000181a99c60 __CFSimpleMergeSort + 232
5 CoreFoundation 0x0000000181a99c60 __CFSimpleMergeSort + 232
6 CoreFoundation 0x0000000181a99c60 __CFSimpleMergeSort + 232
7 CoreFoundation 0x0000000181a99c60 __CFSimpleMergeSort + 232
8 CoreFoundation 0x0000000181a99c60 __CFSimpleMergeSort + 232
9 CoreFoundation 0x0000000181a99c60 __CFSimpleMergeSort + 232
10 CoreFoundation 0x0000000181a99c60 __CFSimpleMergeSort + 232
11 CoreFoundation 0x0000000181a99c60 __CFSimpleMergeSort + 232
12 CoreFoundation 0x0000000181a99c60 __CFSimpleMergeSort + 232
13 CoreFoundation 0x0000000181a99c60 __CFSimpleMergeSort + 232
14 CoreFoundation 0x0000000181a99c60 __CFSimpleMergeSort + 232
15 CoreFoundation 0x00000001819c585c CFSortIndexes + 460
16 CoreFoundation 0x00000001819c6f9c CFMergeSortArray + 372
17 Foundation 0x00000001824d80e8 _sortedObjectsUsingDescriptors + 568
18 Foundation 0x00000001825c64e0 -[NSSet(NSKeyValueSorting) sortedArrayUsingDescriptors:] + 536
19 Eikon 0x0000000100198b98 __51-[EIKNewsHeadlineStoreManager saveHeadlines:block:]_block_invoke (EIKNewsHeadlineStoreManager.m:67)
20 CoreData 0x0000000183e5e214 developerSubmittedBlockToNSManagedObjectContextPerform + 148
21 libdispatch.dylib 0x000000018099a9a0 _dispatch_client_callout + 12
22 libdispatch.dylib 0x00000001809a8ad4 _dispatch_queue_serial_drain + 924
23 libdispatch.dylib 0x000000018099e2cc _dispatch_queue_invoke + 880
24 libdispatch.dylib 0x00000001809a8fa8 _dispatch_queue_override_invoke + 340
25 libdispatch.dylib 0x00000001809aaa50 _dispatch_root_queue_drain + 536
26 libdispatch.dylib 0x00000001809aa7d0 _dispatch_worker_thread3 + 120
27 libsystem_pthread.dylib 0x0000000180ba31d0 _pthread_wqthread + 1092
28 libsystem_pthread.dylib 0x0000000180ba2d7c start_wqthread + 0
What i am trying to do is get some news headlines from server every 5 or 10 secs, sort them in some order using NSSortDescriptors and then store them in my CoreData. And that is what exactly saveHeadlines:block method is doing inside my class.
What exactly objc_msgSend() selector name: length
mean here? Any help is appreciated.
Note: Its a sporadic issue. When i test in simulator or my devices its not crashing. But some users of our app are reporting this crash.
Edited question: inside saveHeadlines:block method
__weak typeof(self) weakSelf = self;
[self.privateMOC performBlock:^{
NSMutableArray *newsTopics = [[EIKNewsTopicStoreManager managedObjectsInManagedObjectContext:self.privateMOC] mutableCopy];
[newsTopics addObjectsFromArray:[EIKNewsTopicStoreManager topicsManagedObjectForNewsFeedInContext:self.privateMOC]];
weakSelf.storedHeadlines = [NSMutableSet set];
for (NSManagedObject *storedTopic in newsTopics) {
[weakSelf.storedHeadlines addObjectsFromArray:[[storedTopic valueForKey:EIKFeedTopicRelationshipItems] allObjects]];
}
NSArray *sortDescriptors = #[[NSSortDescriptor sortDescriptorWithKey:EIKNewsHeadlineAttributeHeadlineId ascending:YES]];
NSArray *orderedItems = [items sortedArrayUsingDescriptors:sortDescriptors];
NSArray *orderedHeadlines = [weakSelf.storedHeadlines sortedArrayUsingDescriptors:sortDescriptors];
And the crash is happening on the last line. i.e
NSArray *orderedHeadlines = [weakSelf.storedHeadlines sortedArrayUsingDescriptors:sortDescriptors];
Not able to figure out why.!
My NSManagedObjectContext is initialised like this[i.e privateMOC as shown in above code]
NSManagedObjectContext *confinementContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
Not much info here, but it looks to me that NSManagedObjectContext isn't valid on the thread you are performing your operation.
Please make sure that you perform all operations on the context's private queue. You can do this for example by using performBlock: or performBlockAndWait:.
Also you can't reuse instances of NSManagedObject on different threads than the one you fetched them on.
Hope that helps.
Related
We are experiencing a weird issue on iOS16. There is no code change, it works fine on lower OS versions.
The stacktrace is:
The first case
Crashed: com.apple.main-thread
EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x00000000e5bde410
0
libobjc.A.dylib
objc_release_x21 + 16
1
CoreData
-[_PFManagedObjectReferenceQueue _processReferenceQueue:] + 1020
2
CoreData
-[NSManagedObjectContext _processRecentChanges:] + 112
3
CoreData
_performRunLoopAction + 412
4
CoreFoundation
__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 36
5
CoreFoundation
__CFRunLoopDoObservers + 532
6
CoreFoundation
__CFRunLoopRun + 1048
7
CoreFoundation
CFRunLoopRunSpecific + 612
8
GraphicsServices
GSEventRunModal + 164
9
UIKitCore
-[UIApplication _run] + 888
10
UIKitCore
UIApplicationMain + 340
The second case, I believe this stacktrace has the same root cause as the previous one (same trend, same UI page, only happens on iOS 16).
Crashed: com.apple.main-thread
EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x0033003200390070
0
libobjc.A.dylib
objc_msgSend + 32
1
CoreFoundation
-[__NSArrayM dealloc] + 188
2
MyApplication
MyClass.m - Line 361
-[MyClass loadMessages:] + 361
3
MyApplication
MyClass.m - Line 125
__74-[MyClass requestRecentMessagesAndDiscardExistingMessagesCompletion:]_block_invoke + 125
4
libdispatch.dylib
_dispatch_call_block_and_release + 32
5
libdispatch.dylib
_dispatch_client_callout + 20
6
libdispatch.dylib
_dispatch_main_queue_drain + 928
7
libdispatch.dylib
_dispatch_main_queue_callback_4CF + 44
8
CoreFoundation
__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 16
9
CoreFoundation
__CFRunLoopRun + 2036
10
CoreFoundation
CFRunLoopRunSpecific + 612
11
GraphicsServices
GSEventRunModal + 164
12
UIKitCore
-[UIApplication _run] + 888
13
UIKitCore
UIApplicationMain + 340
MyClass.m is an Objective-C class, it has a property:
#property (nonatomic, strong) NSArray<Message *> *messages; // Message is NSManagedObject
In the second stacktrace, frame -[OurClass loadMessages:] + 361, the messages array is deallocated:
self.messages = [[NSArray alloc] init...];
So my guess is, somehow the messages are over-released. If the messages are released in MyClass before, then the crash happens as the first stacktrace, otherwise it happens as the second stacktrace.
I've turned on -com.apple.CoreData.ConcurrencyDebug 1 to try to debug this, but no luck.
Any help would be much appreciated. Thanks in advance!
I'm using Magical Record for Core Data operations.
It seems that the crash I posted below would only happen in iOS 7.
And EXC_BAD_ACCESS KERN_INVALID_ADDRESS means some object is deallocated.
I am not quite sure where the bug is.
Any idea on this? Thanks.
Thread : Crashed: NSManagedObjectContext Queue
0 libobjc.A.dylib 0x39d7c636 objc_msgSend + 21
1 Foundation 0x2fee4d9d -[NSError dealloc] + 60
2 libobjc.A.dylib 0x39d81b6b objc_object::sidetable_release(bool) + 174
3 libobjc.A.dylib 0x39d820d7 (anonymous namespace)::AutoreleasePoolPage::pop(void*) + 358
4 CoreFoundation 0x2f4f5c19 _CFAutoreleasePoolPop + 16
5 Foundation 0x2feef637 -[NSAutoreleasePool drain] + 122
6 CoreData 0x2f336fb9 -[NSManagedObjectContext save:] + 944
7 Branch 0x20f87d __70-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:]_block_invoke20 (NSManagedObjectContext+MagicalSaves.m:82)
8 CoreData 0x2f39a935 developerSubmittedBlockToNSManagedObjectContextPerform + 88
9 CoreData 0x2f39aa7b -[NSManagedObjectContext performBlockAndWait:] + 114
10 Branch 0x20f621 -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:] (NSManagedObjectContext+MagicalSaves.m:128)
11 CoreData 0x2f39a935 developerSubmittedBlockToNSManagedObjectContextPerform + 88
12 libdispatch.dylib 0x3a26cdd7 _dispatch_barrier_sync_f_invoke + 26
13 CoreData 0x2f39aa73 -[NSManagedObjectContext performBlockAndWait:] + 106
14 Branch 0x205831 +[MagicalRecord(Actions) saveWithBlockAndWait:] (MagicalRecord+Actions.m:44)
15 Branch 0x1acdbb __62-[BREmployeeDataController getPositionsAtLocation:completion:]_block_invoke (BREmployeeDataController.m:42)
16 Branch 0x1ad87d __66-[BREmployeeDataController synchronizeStaffAtLocation:completion:]_block_invoke (BREmployeeDataController.m:193)
17 Branch 0x1d215b __116-[AFHTTPSessionManager dataTaskWithHTTPMethod:URLString:parameters:uploadProgress:downloadProgress:success:failure:]_block_invoke80 (AFHTTPSessionManager.m:287)
18 Branch 0x1e326b __72-[AFURLSessionManagerTaskDelegate URLSession:task:didCompleteWithError:]_block_invoke_2150 (AFURLSessionManager.m:308)
19 libdispatch.dylib 0x3a25a833 _dispatch_call_block_and_release + 10
20 libdispatch.dylib 0x3a25a81f _dispatch_client_callout + 22
21 libdispatch.dylib 0x3a25a777 _dispatch_main_queue_callback_4CF$VARIANT$up + 254
22 CoreFoundation 0x2f58c8a1 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 8
23 CoreFoundation 0x2f58b175 __CFRunLoopRun + 1300
24 CoreFoundation 0x2f4f5ebf CFRunLoopRunSpecific + 522
25 CoreFoundation 0x2f4f5ca3 CFRunLoopRunInMode + 106
26 GraphicsServices 0x343fb663 GSEventRunModal + 138
27 UIKit 0x31e4214d UIApplicationMain + 1136
28 Branch 0x1a6437 main (main.m:14)
29 libdyld.dylib 0x3a27fab7 start + 2
Are you using ARC? Does MagicalRecord use ARC?
Either you or MagicalRecord is over-releasing some object that an NSAutoreleasePool believes it owns. Then the pool gets popped (when your NSManagedObjectContext finishes the save: operation) and one of the objects in there is garbage. Try running with NSZombies enabled, or use the Allocations Instrument to determine which object is being over-released.
Usually app works great, but i received issue in Crashlytics. It's happen 1-2 times in week. For 2k-3k users. App work on iOS 7 and later.
Report: Fatal Exception: NSInvalidArgumentException
-[UIPasteboard setString:]: Argument is not an object of type NSString [(null)]
Here stack of thread where app crashed:
Thread : Crashed: com.apple.main-thread
0 libsystem_kernel.dylib 0x38fae1f0 __pthread_kill + 8
1 libsystem_pthread.dylib 0x390167b7 pthread_kill + 58
2 libsystem_c.dylib 0x38f5eff9 abort + 76
3 libc++abi.dylib 0x383ad98f abort_message + 74
4 libc++abi.dylib 0x383c66e7 default_terminate_handler() + 254
5 libobjc.A.dylib 0x389f9f7d _objc_terminate() + 192
6 APPNAME 0x001f1495 CPPExceptionTerminate() (KSCrashSentry_CPPException.mm:193)
7 libc++abi.dylib 0x383c41b3 std::__terminate(void (*)()) + 78
8 libc++abi.dylib 0x383c3a09 __cxxabiv1::exception_cleanup_func(_Unwind_Reason_Code, _Unwind_Exception*)
9 libobjc.A.dylib 0x389f9dbb objc_exception_throw + 250
10 CoreFoundation 0x2e25ee0d -[NSException initWithCoder:]
11 UIKit 0x30e3e0eb -[UIPasteboard(UIPasteboardDataExtensions) setString:] + 154
12 APPNAME 0x0011cfaf __33-[WebViewController moreActions:]_block_invoke_2 (WebViewController.m:424)
13 UIKit 0x30c74ceb __56-[UIActivityViewController _cleanupActivityWithSuccess:]_block_invoke + 54
14 UIKit 0x30ba87c9 -[UIWindowController transitionViewDidComplete:fromView:toView:removeFromView:] + 1880
15 UIKit 0x30ba7f9f __101-[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:animation:]_block_invoke293 + 170
16 UIKit 0x30ba7ecb -[_UIViewControllerTransitionContext completeTransition:] + 74
17 UIKit 0x30ba7db1 -[UITransitionView notifyDidCompleteTransition:] + 288
18 UIKit 0x30ba78c1 -[UITransitionView _didCompleteTransition:] + 944
19 UIKit 0x30ac83b7 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 178
20 UIKit 0x30ac82cf -[UIViewAnimationState animationDidStop:finished:] + 66
21 QuartzCore 0x30712e0b CA::Layer::run_animation_callbacks(void*) + 234
22 libdispatch.dylib 0x38ee2d3f _dispatch_client_callout + 22
23 libdispatch.dylib 0x38ee56c3 _dispatch_main_queue_callback_4CF + 278
24 CoreFoundation 0x2e229641 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 8
25 CoreFoundation 0x2e227f0d __CFRunLoopRun + 1308
26 CoreFoundation 0x2e192729 CFRunLoopRunSpecific + 524
27 CoreFoundation 0x2e19250b CFRunLoopRunInMode + 106
28 GraphicsServices 0x331016d3 GSEventRunModal + 138
29 UIKit 0x30af3871 UIApplicationMain + 1136
30 APPNAME 0x000b9287 main (main.m:16)
App crahed in WebViewController in function moreActions.
12 APPNAME 0x0011cfaf __33-[WebViewController moreActions:]_block_invoke_2 (WebViewController.m:424)
This Function show UIActivityViewController and one of custom action - copy link to web page.
__weak WebViewController *weakSelf = self;
[activityViewController setCompletionHandler:^(NSString *activityType, BOOL completed) {
WebViewController *strongSelf = weakSelf;
if ([activityType isEqualToString:#"VK"]) {
// actions
} else if ([activityType isEqualToString:#"copyLink"]) {
// HERE UIPasteboard!
UIPasteboard *pb = [UIPasteboard generalPasteboard];
[pb setString:[strongSelf.startUrl absoluteString]];
}
}];
Make sure that [strongSelf.startUrl absoluteString] does not returns nil just before setting it to pasteboard string. I'm pretty sure that your startUrl apparently becomes nil.
I am getting this crash for my iOS app and it doesn't show any relevant file name (just shows main.m) that I can look at. Any pointers on what the issue could be?
Exception Type: SIGSEGV
Exception Codes: SEGV_ACCERR at 0x7ebf14b8
Crashed Thread: 0
Thread 0 Crashed:
0 libobjc.A.dylib 0x3892860c objc_retain + 12
1 CoreData 0x2defa915 -[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:] + 4013
2 CoreFoundation 0x2e144f41 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 13
3 CoreFoundation 0x2e0b8da9 _CFXNotificationPost + 1721
4 Foundation 0x2eaa3cc5 -[NSNotificationCenter postNotificationName:object:userInfo:] + 73
5 CoreData 0x2def994b -[NSManagedObjectContext(_NSInternalNotificationHandling) _postObjectsDidChangeNotificationWithUserInfo:] + 79
6 CoreData 0x2def98e3 -[NSManagedObjectContext(_NSInternalChangeProcessing) _createAndPostChangeNotification:withDeletions:withUpdates:withRefreshes:] + 299
7 CoreData 0x2def7f9f -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] + 2323
8 CoreData 0x2df61c21 -[NSManagedObjectContext(_NestedContextSupport) _parentProcessSaveRequest:inContext:error:] + 1313
9 CoreData 0x2df6256f __82-[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:]_block_invoke + 563
10 libdispatch.dylib 0x38e08b3b _dispatch_barrier_sync_f_slow_invoke + 71
11 libdispatch.dylib 0x38e02d3f _dispatch_client_callout + 23
12 libdispatch.dylib 0x38e056c3 _dispatch_main_queue_callback_4CF + 279
13 CoreFoundation 0x2e14d681 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
14 CoreFoundation 0x2e14bf4d __CFRunLoopRun + 1309
15 CoreFoundation 0x2e0b6769 CFRunLoopRunSpecific + 525
16 CoreFoundation 0x2e0b654b CFRunLoopRunInMode + 107
17 GraphicsServices 0x330236d3 GSEventRunModal + 139
18 UIKit 0x30a15891 UIApplicationMain + 1137
19 MyApp 0x00034f7f main (main.m:18)
You need to enable the symbolication in order to see where the crash was caused in your code.
For further info check this Apple Documentation or this answer on Stack Overflow
My iOS app is crashing when doing two calls to countForFetchRequest at the same time on the same private queue NSManagedObjectContext using performBlock.
I setup my childContext like this
_childContext = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[_childContext setParentContext:self.managedObjectContext];
And this is my performBlock that calls countForFetchRequest
[self.childContext performBlock:^{
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:#"History"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:#"url == %#", url];
NSError *error = nil;
NSUInteger num = [self.childContext countForFetchRequest:fetchRequest error:&error];
if(error != nil){
NSLog(#"Error getting count for history url %# %#", url, error);
return;
}
if(num > 0){ // Already have this in the history, don't re-add it
return;
}
History *history = (History *)[NSEntityDescription insertNewObjectForEntityForName:#"History" inManagedObjectContext:self.childContext];
history.url = url;
history.title = title;
if(![self.childContext save:&error]){
NSLog(#"Error occurred saving history item %# %# %#", title, url, error);
}
}];
And here is the crash log:
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 libsystem_kernel.dylib 0x3a427350 __pthread_kill + 8
1 libsystem_c.dylib 0x3a39e11e pthread_kill + 54
2 libsystem_c.dylib 0x3a3da96e abort + 90
3 libc++abi.dylib 0x39978d4a abort_message + 70
4 libc++abi.dylib 0x39975ff4 default_terminate() + 20
5 libobjc.A.dylib 0x39f29a74 _objc_terminate() + 144
6 libc++abi.dylib 0x39976078 safe_handler_caller(void (*)()) + 76
7 libc++abi.dylib 0x39976110 std::terminate() + 16
8 libc++abi.dylib 0x39977594 __cxa_rethrow + 84
9 libobjc.A.dylib 0x39f299cc objc_exception_rethrow + 8
10 CoreData 0x31e868e0 -[NSManagedObjectContext(_NSInternalAdditions) _countWithNoChangesForRequest:error:] + 764
11 CoreData 0x31e833fe -[NSManagedObjectContext countForFetchRequest:error:] + 1062
12 CoreData 0x31e92470 __82-[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:]_block_invoke_0 + 460
13 libdispatch.dylib 0x3a345d26 _dispatch_barrier_sync_f_slow_invoke + 82
14 libdispatch.dylib 0x3a3404b4 _dispatch_client_callout + 20
15 libdispatch.dylib 0x3a3451b8 _dispatch_main_queue_callback_4CF$VARIANT$mp + 220
16 CoreFoundation 0x3205cf36 __CFRunLoopRun + 1286
17 CoreFoundation 0x31fcfeb8 CFRunLoopRunSpecific + 352
18 CoreFoundation 0x31fcfd44 CFRunLoopRunInMode + 100
19 GraphicsServices 0x35b992e6 GSEventRunModal + 70
20 UIKit 0x33ee52fc UIApplicationMain + 1116
21 Accountable2You Mobile 0x000e5d28 0xe4000 + 7464
22 Accountable2You Mobile 0x000e5cc4 0xe4000 + 7364
Thread 1 name: Dispatch queue: NSManagedObjectContext Queue
Thread 1:
0 libsystem_kernel.dylib 0x3a416f04 semaphore_wait_trap + 8
1 libdispatch.dylib 0x3a3462fc _dispatch_thread_semaphore_wait$VARIANT$mp + 8
2 libdispatch.dylib 0x3a34487c _dispatch_barrier_sync_f_slow + 96
3 CoreData 0x31e82df2 _perform + 166
4 CoreData 0x31e921c6 -[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:] + 238
5 CoreData 0x31e86770 -[NSManagedObjectContext(_NSInternalAdditions) _countWithNoChangesForRequest:error:] + 396
6 CoreData 0x31e833fe -[NSManagedObjectContext countForFetchRequest:error:] + 1062
7 Accountable2You Mobile 0x000ee7be 0xe4000 + 42942
8 CoreData 0x31e86072 developerSubmittedBlockToNSManagedObjectContextPerform_privateasync + 66
9 libdispatch.dylib 0x3a344eca _dispatch_queue_drain$VARIANT$mp + 138
10 libdispatch.dylib 0x3a344dbc _dispatch_queue_invoke$VARIANT$mp + 36
11 libdispatch.dylib 0x3a34591a _dispatch_root_queue_drain + 182
12 libdispatch.dylib 0x3a345abc _dispatch_worker_thread2 + 80
13 libsystem_c.dylib 0x3a375a0e _pthread_wqthread + 358
14 libsystem_c.dylib 0x3a3758a0 start_wqthread + 4
Am I using the performBlock correctly?
Edit: More details
The performBlock is being called in a webViewDidFinishLoad:webView delegate method. I have multiple UIWebViews and when they finish loading they are calling the delegate method which is in turn calling the performBlock to see if it needs to add the url to the core data database.
You can encapsulate the method causing the crash in:
#synchronized(self) {
[object yourMethod];
}
This will make sure that even while multithreading, the piece of code inside will be running just once at a time ... other threads will just have to wait. Frankly, this will work only if the cause of the crash is the concurrency.