Search code examples
iosxcodeexceptionexc-bad-access

What does that code mean in exception error "Thread 3: EXC_BAD_ACCESS (code=2, address=0x16dbc7ff0)"?


I got a run time error during a debug run of my Xcode project for iOS. The project was running on an iPhone 8 device running iOS 15.5. I am using Xcode version 13.4.1 on a Mac Pro running macOS Monterey version 12.5.

The error message occurred at a line of code where I initialize a custom struct.

Thread 3: EXC_BAD_ACCESS (code=2, address=0x16dbc7ff0)

Here is my relevant code. The error message shows in red where GroupLocalCache is being initialized.

import Foundation
import CloudKit

// Wrap a CKRecordZone and its caches (each record type should have its records saved in the caches.
//
final class Zone {
    let cloudKitZone: CKRecordZone
    let caches = [BaseLocalCache]()
    private var groupCache: GroupLocalCache
    
    init(cloudKitZone: CKRecordZone, container: CKContainer, cloudKitDB: CKDatabase) {
        self.cloudKitZone = cloudKitZone
        self.groupCache = GroupLocalCache(container: container, cloudKitDB: cloudKitDB, cloudKitZone: cloudKitZone)
    }
    
}

Here is the code for GroupLocalCache:

final class GroupLocalCache: BaseLocalCache {
    let container: CKContainer
    let cloudKitDB: CKDatabase
    let zone: Zone
    
    private var serverChangeToken: CKServerChangeToken?
    private var groups = [Group]()
    
    init(container: CKContainer, cloudKitDB: CKDatabase, cloudKitZone: CKRecordZone) {
        self.container = container
        self.cloudKitDB = cloudKitDB
        self.zone = Zone(cloudKitZone: cloudKitZone, container: container, cloudKitDB: cloudKitDB)
        
        super.init()
        
        fetchChanges() // Fetching changes with a nil token to build up the cache.
    }
}

BaseLocalCache is a modified version of the BaseLocalCache class that comes with CloudKitShare Xcode project from this Apple webpage: Sharing CloudKit Data with Other iCloud Users.

I want to look up what that "2" represents in the message where it says "code=2".

Other posts on stackoverflow* and Apple's Developer Forums for this same error address specific situations different than the error in my code.

I would like to know what the low level cause of the error is, so I need to know what those codes are that show up in exception error messages, since they tell what the real cause of exception error are in a lower level than answers to posts about exception errors commonly found.

I think I found documentation at one time for those codes at another source outside of Apple. It seems there are codes that are used by hardware manufacturers for "architectures" such as "arm64" and "armv7". I see those in Xcode project build settings.

Do I really need to know what each code as given in the error message represent?

I found this article at Apple's Developer Documentation:

Understanding the Exception Types in a Crash Report

I found this documentation on Apple's Exception Handling Framework in a search with Google: Exception Handling. I don't see what I need. This framework seems to be more than I need at this time.


Solution

  • You currently have an infinite loop of object creations. That's why you get a crash.

    When crashing, do not hesitate to see the callstack, you should see it clearly.

    The Zone initialization, calls a GroupLocalCache initialization, which calls a Zone initialization, which will call a GroupLocalCache initialization, etc.

    So you need to change the logic on your objects.