_PFObjectIDFastHash64 Crash EXC_BAD_ACCESS KERN_INVALID_ADDRESS

Since the release of iOS 14, I keep getting reports of this crash via Crashlytics and I have no idea how to figure out what is going wrong, because the stack trace doesn't touch my code anywhere. What can I do? How do I find the source of the problem?

Code Block
EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x0000000000000004
Crashed: com.apple.root.user-initiated-qos
0 CoreData 0x1b85a87ac _PFObjectIDFastHash64 + 40
1 CoreFoundation 0x1b254e4f8 __CFBasicHashRehash + 992
2 CoreFoundation 0x1b2552414 CFBasicHashRemoveValue + 2384
3 CoreFoundation 0x1b2469ec0 CFDictionaryRemoveValue + 236
4 CoreData 0x1b84f51c0 -[NSManagedObjectContext(_NSInternalAdditions) _forgetObject:propagateToObjectStore:removeFromRegistry:] + 124
5 CoreData 0x1b84d46ec -[_PFManagedObjectReferenceQueue _processReferenceQueue:] + 860
6 CoreData 0x1b85a0734 -[_PFAutoreleasePoolThunk dealloc] + 48
7 libobjc.A.dylib 0x1c65bb81c AutoreleasePoolPage::releaseUntil(objc_object**) + 204
8 libobjc.A.dylib 0x1c65bb6e8 objc_autoreleasePoolPop + 212
9 libdispatch.dylib 0x1b2120aa4 _dispatch_last_resort_autorelease_pool_pop + 44
10 libdispatch.dylib 0x1b21313c8 _dispatch_root_queue_drain + 1064
11 libdispatch.dylib 0x1b21318e8 _dispatch_worker_thread2 + 116
12 libsystem_pthread.dylib 0x1f9a748cc _pthread_wqthread + 216
13 libsystem_pthread.dylib 0x1f9a7b77c start_wqthread + 8

Same problem on iOS14, build with Xcode12. does anybody know how to fix it?
I'm experiencing this issue for at least a month now as well. I was watching this issue and hoping for a solution.

In my case Firebase shows thousands of crashes related to CoreData or CoreFoundation on one of the following:

"PFObjectIDFastHash64"
"-[NSManagedObjectContext
disposeObjects:count:notifyParent:]"
"CFBasicHashRemoveValue"

It's only iOS 14 and although I don't know how reliable the data is, but the majority seems to be on iPhone.

I'm not able to reproduce it and haven't heard any significant complaints from our helpdesk either.
We are seeing this as well. The only thing we really changed was we built the code against ios 14 and released it to the users. Raygun is reporting lots of crashes, but only a few users are reporting them to us. I checked and we have PerformBlackandWait around the MOC save calls. We have Parent and Child MOC so the app can sync data to the cloud etc.
The crashes happen on the parent MOC Save. We also get errors like

Exception EXCBADACCESS, Code 190968208, Subcode 8 Attempted to dereference garbage pointer 0x50b61f190. 

The file “MyAppDatabase.sqlite” couldn’t be opened.

An error occurred while saving. 

We are going to revert back to xcode 11.7 until apple can fix this.


So as mentioned below (with explanation) these should be debugged with -com.apple.CoreData.ConcurrencyDebug 1

Previously asymptomatic multi-threading bugs may now be symptomatic on iOS 14. This is a consequence of additional memory optimizations in iOS 14. Apps with these bugs get the previous less optimized behavior when linked for iOS 13 or older releases.
I try to change my NSManagedObjectContext concurrencyType from [.privateQueueConcurrencyType] to [.mainQueueConcurrencyType]
is working.not crash anymore.
Here are a few things I found that helped.

I have 3 MOCs. The Private(root/parent) then two, one for UI and one for data Sync engine( no tracking). I did as  SiWeiLiFox posted and no longer used NSPrivateQueueConcurrencyType on my SyncingMoc and Private(root/parent) Moc. This solved the Random excbadaccess I would get on   
Code Block
[[self managedObjectContext] refreshAllObjects];

or when running

Code Block
     if (![moc save:&error])


the frustrating thing about the one above is that it crashes before it runs the moc save so you cant even catch the error.
It can also lead to data loss because the error might not show and all data changes after these errors are on the data stack might never be committed to the disk so they are lost upon app restart.

The biggest thing I found was passing objects to Views for editing, then using the object.ManageObjectContext to save the object. The object.moc is a weak reference and GC in ios 14 really likes to throw the moc away.

I migrated my UI to a shared instance of the MOC and combed the code over and over again for the references to object.moc and the app is more stable than it was before ios14. I'm not sure what blog I picked up the habit of using object.moc but its apparently a bad one.

Good luck to all with these issues, I hope some of these tips help others.


Any updates on this issue ? Do I really need to stop using child contexts with NSPrivateQueueConcurrencyType ?
I use the shared context returned by below method to use across app(working since 2 yrs, started crashing with Xcode 12.3).

Code Block
-(NSManagedObjectContext *) getCurrentContext
{
  NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
  if ((coordinator != nil) && iManagedObjectContext == nil){
    iManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    [iManagedObjectContext setPersistentStoreCoordinator:coordinator];
  }
   
  if (_mainManagedObjectContext == nil){
    _mainManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
    [_mainManagedObjectContext setParentContext:iManagedObjectContext];
  }
   
  if (privateObjectContext == nil){
    privateObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    privateObjectContext.parentContext = _mainManagedObjectContext;
  }
  return privateObjectContext;
   
}

I was getting this crash intermittently as others have described, and I believe it was triggered by using a background context on the main thread. I have a class that wraps all CoreData interactions, and it uses NSPersistentContainer to accomplish this. I was using NSPersistentContainer.viewContext for everything which was causing intermittent issues when I accessed it from a background thread. I then added a member called backgroundContext that is initialized using NSPersistentContainer.newBackgroundContext(), and using that for everything is when I ran into the issue that we're all here for. So I added a computed value that uses the appropriate context based on the current thread:
private var context: NSManagedObjectContext { Thread.isMainThread ? container.viewContext : backgroundContext }
and I use that whenever I need an NSManagedObjectContext.

I have not been able to reproduce any CoreData crashes since I made this change. I hope this is helpful to somebody. I'll put the relevant part of my class below for reference.

Code Block
final class CoreDataManager {
  // MARK: Singleton
  static let shared = CoreDataManager()
  private init() { backgroundContext = container.newBackgroundContext() }
   
  // MARK: Constants
  private let container: PersistentContainer = {
    let container = PersistentContainer(name: "LocalModel")
    container.loadPersistentStores { description, error in
      if let error = error {
        CriticalError.handle(with: "Unable to load persistent stores: \(error)")
      }
    }
    return container
  }()
   
  private let backgroundContext: NSManagedObjectContext
   
  // MARK: Computed Values
  private var context: NSManagedObjectContext { Thread.isMainThread ? container.viewContext : backgroundContext }
}

I am facing same problem after iOS 14.0 version(build with xcode 12). See my crash log
Code Block
Crashed: NSManagedObjectContext 0x283348410
0 CoreData 0x1a9b1ba6c _PFObjectIDFastHash64 + 36
1 CoreFoundation 0x1a3cd8aa8 CFBasicHashRehash + 996
2 CoreFoundation 0x1a3cdc790 CFBasicHashRemoveValue + 2352
3 CoreFoundation 0x1a3bfa600 CFDictionaryRemoveValue + 224
4 CoreData 0x1a9a6b390 -[NSManagedObjectContext(_NSInternalAdditions) _forgetObject:propagateToObjectStore:removeFromRegistry:] + 120
5 CoreData 0x1a9a4aef8 -[_PFManagedObjectReferenceQueue _processReferenceQueue:] + 864
6 CoreData 0x1a9b63a7c 90-[NSManagedObjectContext(_NSInternalNotificationHandling) _registerAsyncReferenceCallback]_block_invoke + 68
7 CoreData 0x1a9b5a01c developerSubmittedBlockToNSManagedObjectContextPerform + 156
8 libdispatch.dylib 0x1a38e7280 _dispatch_client_callout + 16
9 libdispatch.dylib 0x1a38c34f0 _dispatch_lane_serial_drain$VARIANT$armv81 + 568
10 libdispatch.dylib 0x1a38c3fdc _dispatch_lane_invoke$VARIANT$armv81 + 404
11 libdispatch.dylib 0x1a38cd800 _dispatch_workloop_worker_thread + 692
12 libsystem_pthread.dylib 0x1ec3e35a4 _pthread_wqthread + 272
13 libsystem_pthread.dylib 0x1ec3e6874 start_wqthread + 8

I was having similar problems. All seems to be fine since Xcode 12.4
Having same issue. Any solution on this yet?
I too have same type of crashes. Tried all possible solutions in this thread, but no luck. Waiting for better solution.
Will try @simonearls solution.
having the same crash on Crashlytics ,Any solution on this yet?
Same, also happens on Apple Watch running watchOS 7
I am also having same issue after switching to XCode 12.2. @mcalapatapu@nowcom.in have you tried using XCode 12.4? is it resolved in 12.4?
_PFObjectIDFastHash64 Crash EXC_BAD_ACCESS KERN_INVALID_ADDRESS
 
 
Q