Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXC_BAD_ACCESS(code=1, address=0x10) on Core Data Fetch

I'm getting an intermittent bug that is proving very hard to debug.

I'm getting the following error from the following method

EXC_BAD_ACCESS(code=1, address=0x10) on Core Data Fetch

class func getAll(context: NSManagedObjectContext) -> [Tag] {
    var returnValue: [Tag] = []
    do {
        let fetchRequest = NSFetchRequest(entityName: Tag.entityName())
        returnValue = try context.executeFetchRequest(fetchRequest) as! [Tag]
    } catch {
    }
    return returnValue
}

The bug is extermely intermittent, and is only happening on every few 100 sessions, but is appearing frequent enough that I need to deal with it. The code breaks on the line returnvalue = try context.execute...

  • From debugging, my fetchRequest is not nil
  • my context is not nil
  • returnValue has a default value of an empty array
  • my backgroundContext is running on a background thread

I've turned on the NSZombieFlag to try to see if some memory is deallocated somewhere and then being accessed, but I'm stumped on what is causing this. Any ideas or insight would be much appreciated.

like image 432
Unome Avatar asked Mar 25 '16 14:03

Unome


People also ask

What does thread 1 Exc_bad_access mean?

EXC_BAD_ACCESS means that message was sent to a point in the memory where there's no instance of a class to execute it. Thus “bad access”. You will get EXC_BAD_ACCESS in 3 cases: An object is not initialized.

How to debug EXC_ BAD_ access ios?

To debug an EXC_BAD_ACCESS, you can generally find out the where the dangling pointer is by enabling zombie objects. Choose edit scheme, then Diagnostics tab in the Run section, then click the 'Zombie Objects' option. Another cause for EXC_BAD_ACCESS can be infinite recursion, which can be found by adding some logging.


1 Answers

Almost all EXC_BAD_ACCESS issues I've seen with Core Data are caused by trying to use thread concurrency instead of the newer queue concurrency model.

Since iOS 5 you are required to use performBlock or performBlockAndWait when accessing a managed object context.

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/Concurrency.html

like image 63
Mathew Spolin Avatar answered Sep 27 '22 02:09

Mathew Spolin