Core Data EXC_BAD_ACCESS KERN_PROTECTION_FAILURE - ios

I have multiple crash reports that point to the following:
Thread : Crashed: NSOperationQueue 0x18c7fba0
0 libsystem_platform.dylib 0x38665a36 OSAtomicCompareAndSwap32Barrier + 13
1 libobjc.A.dylib 0x3805694b realizeClass(objc_class*) + 78
2 libobjc.A.dylib 0x38058797 lookUpImpOrForward + 74
3 libobjc.A.dylib 0x3805102b _class_lookupMethodAndLoadCache3 + 34
4 libobjc.A.dylib 0x38050df9 _objc_msgSend_uncached + 24
5 CoreData 0x2da7b5bb -[_NSFaultingMutableSet copyWithZone:] + 238
6 MyApp 0x0027226f -[Zoo getSortedCats] (Zoo.m:63)
7 MyApp 0x00286955 -[BlockExecutionOperation main] (BlockExecutionOperation.m:30)
8 Foundation 0x2e627aa5 -[__NSOperationInternal _start:] + 772
9 Foundation 0x2e6cb96d __NSOQSchedule_f + 60
10 libdispatch.dylib 0x3853e4b7 _dispatch_async_redirect_invoke + 110
11 libdispatch.dylib 0x3853f7d9 _dispatch_root_queue_drain + 224
12 libdispatch.dylib 0x3853f9c5 _dispatch_worker_thread2 + 56
13 libsystem_pthread.dylib 0x38669dff _pthread_wqthread + 298
The getSortedCats method looks like this:
- (NSArray *)getSortedCats {
NSSet* cats = [self.cats copy]; //this is line 63, where the crash occurs
//do some sorting
return sortedCats;
}
And Zoo is an NSManagedObject subclass with a cats property:
#property (atomic, retain) NSSet *cats;
So why the crash on the self.cats line? What does this error mean? How to avoid? It only happens once in a while, and is not reproducible.

Peculiarity with Core Data. Sending copy would just return another NSFaultingMutableSet, and Core Data is too buggy to do that right.
Replace it with [NSSet setWithSet:self.cats] and you'd face lesser errors.

Related

Coredata crash on iOS 16, over-release in -[_PFManagedObjectReferenceQueue _processReferenceQueue:]

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!

CoreData _NSIsNSSet EXC_BAD_ACCESS KERN_INVALID_ADDRESS - crash on app launch

We are getting this crash on multiple devices but cannot figure out where it originates from. The main thread is crashing.
Stack trace:
Crashed: com.apple.main-thread
0 libobjc.A.dylib 0x193dfd58c object_getMethodImplementation + 20
1 CoreFoundation 0x1940f6928 _NSIsNSSet + 40
2 CoreFoundation 0x193fc2c04 -[NSMutableSet unionSet:] + 112
3 CoreData 0x198abe014 -[_NSFaultingMutableSet willReadWithContents:] + 668
4 CoreData 0x198ae4280 -[_NSFaultingMutableSet count] + 32
5 CoreData 0x198be1bb4 __107-[NSManagedObjectContext(_NestedContextSupport) newValueForRelationship:forObjectWithID:withContext:error:]_block_invoke + 360
6 CoreData 0x198be2e70 internalBlockToNSManagedObjectContextPerform + 104
7 libdispatch.dylib 0x193d8b5ac _dispatch_client_callout + 20
8 libdispatch.dylib 0x193d9843c _dispatch_async_and_wait_invoke + 96
9 libdispatch.dylib 0x193d8b5ac _dispatch_client_callout + 20
10 libdispatch.dylib 0x193d977d4 _dispatch_main_queue_callback_4CF + 832
11 CoreFoundation 0x1940648d4 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 16
12 CoreFoundation 0x19405f58c __CFRunLoopRun + 1692
13 CoreFoundation 0x19405ebc8 CFRunLoopRunSpecific + 480
14 GraphicsServices 0x19e4475cc GSEventRunModal + 164
15 UIKitCore 0x198211744 UIApplicationMain + 1936
16 My App 0x100b37324 main + 16 (main.m:16)
17 libdyld.dylib 0x193edb384 start + 4
At the same time, a background thread operating in a PrivateQueue ManagedObjectContext is busy accessing a ManagedObject via awakeFromFetch, stack trace of this thread:
NSManagedObjectContext 0x28066c700
0 libsystem_kernel.dylib 0x1b09617e4 __ulock_wait + 8
1 libdispatch.dylib 0x1b081bf48 _dlock_wait + 56
2 libdispatch.dylib 0x1b081bcf4 _dispatch_thread_event_wait_slow + 56
3 libdispatch.dylib 0x1b0828618 __DISPATCH_WAIT_FOR_QUEUE__ + 336
4 libdispatch.dylib 0x1b0828204 _dispatch_sync_f_slow + 144
5 CoreData 0x1b56686cc _perform + 200
6 CoreData 0x1b558e2c4 -[NSManagedObjectContext(_NestedContextSupport) newValueForRelationship:forObjectWithID:withContext:error:] + 156
7 CoreData 0x1b555033c -[NSFaultHandler retainedFulfillAggregateFaultForObject:andRelationship:withContext:] + 428
8 CoreData 0x1b554df10 -[_NSFaultingMutableSet willReadWithContents:] + 408
9 CoreData 0x1b55ea5bc -[_NSFaultingMutableSet allObjects] + 32
10 My App 0x1044fdc3c PSUser.isAdmin.getter + 286 (PSUser.swift:286)
11 My App 0x1044fdb04 #objc PSUser.isAdmin.getter + 4309900036 (<compiler-generated>:4309900036)
12 My App 0x1044fc694 PSUser.initCurrentTeam() + 212 (PSUser.swift:212)
13 My App 0x1044fcbc0 #objc PSUser.initCurrentTeam() + 4309896128 (<compiler-generated>:4309896128)
14 My App 0x1044fc658 #objc PSUser.awakeFromFetch() + 4309894744 (<compiler-generated>:4309894744)
15 CoreData 0x1b56520e4 _PFFaultHandlerFulfillFault + 3168
16 CoreData 0x1b5650ab0 _PFFaultHandlerLookupRow + 908
17 CoreData 0x1b56527fc _PF_FulfillDeferredFault + 260
18 CoreData 0x1b5666a2c _pvfk_header + 120
19 CoreData 0x1b5663218 _sharedIMPL_pvfk_core + 32
20 My App 0x10438918c +[PSAppSettings isAutoPilotAllowed:] + 97 (PSAppSettings.m:97)
21 My App 0x1043fbb9c LocationTracker.updateTrackingState(user:) + 1120 (LocationTracker.swift:1120)
22 My App 0x104413b4c partial apply for closure #1 in LocationTracker.store(filteredLocations:) + 1275 (LocationTracker.swift:1275)
23 My App 0x1043bae3c thunk for #escaping #callee_guaranteed () -> () + 4308577852 (<compiler-generated>:4308577852)
24 CoreData 0x1b566b650 developerSubmittedBlockToNSManagedObjectContextPerform + 164
25 libdispatch.dylib 0x1b081b5ac _dispatch_client_callout + 20
26 libdispatch.dylib 0x1b0821a64 _dispatch_lane_serial_drain + 568
27 libdispatch.dylib 0x1b0822498 _dispatch_lane_invoke + 400
28 libdispatch.dylib 0x1b082ba5c _dispatch_workloop_worker_thread + 584
29 libsystem_pthread.dylib 0x1b0881718 _pthread_wqthread + 276
30 libsystem_pthread.dylib 0x1b08879c8 start_wqthread + 8
I am not certain if the two stacks are related, or if the background thread is dispatching anything to the main thread (why should it?), but the call newValueForRelationship:forObjectWithID:withContext:error does appear in both stack traces.
The method for awakeFromFetch is initializing a calculated object property currentTeam via the initCurrentTeam() method. This method accesses the teams relationship on that object. Here is the relevant code:
#objc extension PSUser {
override open func awakeFromFetch() {
super.awakeFromFetch()
initCurrentTeam()
}
/**
This method sets the currentTeam by picking
the appropriate team from the `teams` set.
*/
func initCurrentTeam() {
if (isAdmin || isManager), !isEmployee,
var teams = teams?.allObjects as? [PSTeam],
currentTeam == nil {
...
currentTeam = teams.first
}
}
...
}
Enabled com.apple.CoreData.ConcurrencyDebug in Xcode Scheme but Xcode console does not complain. I have not been able to reproduce this issue while debugging in Xcode. The issue is only reported via Crashlytics in production environment.
Any ideas what might be causing this?

SIGSEGV : Sporadic crash issue

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.

Coredata _clearRawPropertiesWithHint crash ios9

I have been stuck with this crash for few days and seem to be going nowhere.
Crashed: NSManagedObjectContext 0x14f016920
EXC_BAD_ACCESS KERN_INVALID_ADDRESS at 0x0000000000000010
Thread : Crashed: NSManagedObjectContext 0x14f016920
0 libobjc.A.dylib 0x000000019953dbd0 objc_msgSend + 16
1 CoreFoundation 0x0000000184969c84 __CFBasicHashDrain + 276
2 CoreFoundation 0x0000000184814da4 CFRelease + 256
3 CoreData 0x0000000184632cf0 -[_NSFaultingMutableSet dealloc] + 68
4 CoreData 0x00000001845f8d3c -[_CDSnapshot dealloc] + 84
5 CoreData 0x00000001846068e0 -[NSManagedObject(_NSInternalMethods) _clearRawPropertiesWithHint:] + 388
6 CoreData 0x00000001846066bc -[NSFaultHandler turnObject:intoFaultWithContext:] + 448
7 CoreData 0x0000000184603e10 -[NSManagedObject dealloc] + 104
8 CoreData 0x0000000184603428 -[_PFManagedObjectReferenceQueue _processReferenceQueue:] + 1076
9 CoreData 0x0000000184681678 __89-[NSManagedObjectContext(_NSInternalNotificationHandling) _registerAyncReferenceCallback]_block_invoke + 104
10 CoreData 0x000000018467bc20 developerSubmittedBlockToNSManagedObjectContextPerform + 196
11 libdispatch.dylib 0x0000000199d25770 _dispatch_client_callout + 16
12 libdispatch.dylib 0x0000000199d3175c _dispatch_queue_drain + 864
13 libdispatch.dylib 0x0000000199d29274 _dispatch_queue_invoke + 464
14 libdispatch.dylib 0x0000000199d25770 _dispatch_client_callout + 16
15 libdispatch.dylib 0x0000000199d33bb0 _dispatch_root_queue_drain + 2140
16 libdispatch.dylib 0x0000000199d3334c _dispatch_worker_thread3 + 112
17 libsystem_pthread.dylib 0x0000000199f39478 _pthread_wqthread + 1092
Any pointers on how I can debug this crash? The same didn't crash in iOS 8. Could it be a iOS 9 crash and hopefully it will be fixed in the subsequent iOS versions?
Any help would be much appreciated.

RestKit crash -- RKManagedObjectRequestOperation.m line 873

I am getting a crash report from my app in production. The stack trace doesn't include any of my code, rather points to code in RestKit itself where it is crashing. The crash itself is hard to reproduce on my side, and seems like some user in production is hitting an edge case. Below is the stack trace:
Thread : Crashed: NSManagedObjectContext 0x1743ecd00
0 libobjc.A.dylib 0x000000019361c0b4 objc_retain + 20
1 MyApp 0x00000001001e64c4 __78-[RKManagedObjectRequestOperation obtainPermanentObjectIDsForInsertedObjects:]_block_invoke (RKManagedObjectRequestOperation.m:873)
2 CoreData 0x0000000181b7d218 developerSubmittedBlockToNSManagedObjectContextPerform + 200
3 libdispatch.dylib 0x0000000193c55954 _dispatch_client_callout + 16
4 libdispatch.dylib 0x0000000193c5f1e4 _dispatch_barrier_sync_f_invoke + 76
5 CoreData 0x0000000181b7d0d8 -[NSManagedObjectContext performBlockAndWait:] + 244
6 MyApp 0x00000001001e62d8 -[RKManagedObjectRequestOperation obtainPermanentObjectIDsForInsertedObjects:] (RKManagedObjectRequestOperation.m:870)
7 MyApp 0x00000001001e34e4 __79-[RKManagedObjectRequestOperation performMappingOnResponseWithCompletionBlock:]_block_invoke345 (RKManagedObjectRequestOperation.m:643)
8 MyApp 0x000000010021c31c -[RKResponseMapperOperation willFinish] (RKResponseMapperOperation.m:317)
9 MyApp 0x000000010021c980 -[RKResponseMapperOperation main] (RKResponseMapperOperation.m:404)
10 Foundation 0x0000000182c601cc -[__NSOperationInternal _start:] + 636
11 Foundation 0x0000000182d21f28 __NSOQSchedule_f + 228
12 libdispatch.dylib 0x0000000193c55954 _dispatch_client_callout + 16
13 libdispatch.dylib 0x0000000193c600a4 _dispatch_queue_drain + 1448
14 libdispatch.dylib 0x0000000193c58a5c _dispatch_queue_invoke + 132
15 libdispatch.dylib 0x0000000193c62318 _dispatch_root_queue_drain + 720
16 libdispatch.dylib 0x0000000193c63c4c _dispatch_worker_thread3 + 108
17 libsystem_pthread.dylib 0x0000000193e3522c _pthread_wqthread + 816
18 libsystem_pthread.dylib 0x0000000193e34ef0 start_wqthread + 4
The crash message itself is:
Crashed: NSManagedObjectContext 0x17198900
EXC_BAD_ACCESS KERN_INVALID_ADDRESS at 0x915da43f
Any hints/directions as to how I can go about debugging this issue?
Thanks.
Ran into this, had setup the core data stack twice on accident, removing the second initialization fixed the issue.

Resources