'EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x0000000000000000 on JSONDecoder.decode

I can see the exc bad excess on crashylitics , following is the code base . Is there anything wrong I am doing ? Crash is occurring on following line


guard let data = userData, let userDetails = try? JSONDecoder().decode(UserDetailsThreadSafe.self, from: data) else {


class UserDetailsThreadSafe:UserDetails
{

    let queue = DispatchQueue(label: "auth-thread-safe-queue", attributes: .concurrent)

    static let shared = UserDetailsThreadSafe()

    private override init()
    {
        super.init()
    }

    required init(from decoder: Decoder) throws {
        try super.init(from: decoder)
    }
    
    fileprivate func getUserDetails() -> UserDetailsThreadSafe? {
        
        queue.sync {
            // perform read and assign value
            var userData: Data? = LoginAuth.sharedInstance().defaultStorage?.secureValue(forKey: kUserDetails)
            if  userData == nil  {
                userData = UserDefaults.standard.value(forKey: kUserDetails) as? Data
            }
            guard let data = userData, let userDetails = try? JSONDecoder().decode(UserDetailsThreadSafe.self, from: data) else {
                return nil
            }
            return userDetails
        }
       
    }
    
    fileprivate func archieveData() {
        
        queue.async(flags: .barrier) {
            // perform writes on data
            if let data = try? JSONEncoder().encode(self), data.isEmpty == false, let jsonString = String(data: data, encoding: .utf8) {
                LoginAuth.sharedInstance().defaultStorage?.setSecure(jsonString, forKey: kUserDetails)
            }
        }
       
    }
}



Solution 1:[1]

When adding tasks to your queue it is recommended to use "async" with the Barrier flag:

queue.async(flags: .barrier) {...}

Since we use async and not sync, the signature of getUserDetails() should change. This function should now pass back the result values using completion block, and return void.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Arik Segal