EXC_BAD_ACCESS code= 2 when dealing with CoreData
Asked Answered
L

1

7

I have recently been getting the error every time I run my app without fail: Thread 1: EXC_BAD_ACCESS (code=2, address=0x16ee77ff4)

func loadTrays(with request: NSFetchRequest<Tray> = Tray.fetchRequest()) {

    do {
        trayArray = try context.fetch(request)
        trayArray = trayArray.reversed()
    } catch {
        print("Error fetching data from context: \(error)")
    }
    trayCollectionView.reloadData()
}

The line that is highlighted by the error is:

trayArray = try context.fetch(request)

I've spent hours looking through SO and Google for a solution but I haven't found one. I've also enabled Zombie objects in the Scheme but I don't get any output into the console. I realize that this error typically comes from pointers trying to access deallocated memory or that the pointer may be corrupt. Furthermore, if there are any resources that anyone can recommend to practice good memory management skills / concepts, please let me know. Additionally, I notice that in the debug navigator, the CPU usage is 99%. Not sure if this is related? I appreciate any help given.

Replying to Gigi's comment:

Persistent Container (AppDelegate):

lazy var persistentContainer: NSPersistentContainer = {

    let container = NSPersistentContainer(name: "Trayful_V2")
    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error as NSError? {

            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
    })
    return container
}()

I declare the context before "override init" in a class with subclass UIView that I am using as a popup:

    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
Lundgren answered 30/11, 2019 at 0:26 Comment(5)
How and where you created the managed object context? The golden rule is “one thread one context”.Basalt
@Basalt are you talking about where it is in the code that I create new objects or are you asking where I create the context itself?Lundgren
I mean where and how you created the managed object context :)Basalt
@Basalt Hi, please see my recent edit on the post. I replied to your comment with code :DLundgren
Try to embed your fetch code in a performBolockAndWait block, I hope this will help.Basalt
H
9

For me it was the name of an CoreData entity attribute. It is not allowed to start an attribute name with "new...". Nothing in Xcode does tell you that and it took me days to find out. I just got the EXC_BAD_ACCESS error.

Just wanted to leave this here for the next poor soul facing my specific problem ;)

The reason for this is explained here: https://developer.apple.com/library/archive/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html#//apple_ref/doc/uid/TP40011226-CH1-SW14

I came across this article that lead me to the solution: https://alexj.org/11/core-data-attribute-naming

Homunculus answered 1/2, 2023 at 11:41 Comment(1)
This was it! Thanks @Homunculus and stackoverflow.Alsworth

© 2022 - 2024 — McMap. All rights reserved.