EXC_BAD_ACCESS (SIGBUS) KERN_PROTECTION_FAILURE when calling Singleton's function inside a Timer's closure

55 views Asked by At

I got this crash log from App Store. This crash only occurred one time to one user about a week ago and after that it never happened again.

On Crashlytics, the crash listed as EXC_BAD_ACCESS KERN_INVALID_ADDRESS.

Below is the crash log from Xcode Organizer:

Exception Type:  EXC_BAD_ACCESS (SIGBUS)
Exception Subtype: KERN_PROTECTION_FAILURE at 0x0000002220200a98
Exception Codes: 0x0000000000000002, 0x0000002220200a98
VM Region Info: 0x2220200a98 is in 0x1000000000-0x7000000000;  bytes after start: 77848382104  bytes before end: 334468478311
      REGION TYPE                 START - END      [ VSIZE] PRT/MAX SHRMOD  REGION DETAIL
      commpage (reserved)      fc0000000-1000000000 [  1.0G] ---/--- SM=NUL  ...(unallocated)
--->  GPU Carveout (reserved) 1000000000-7000000000 [384.0G] ---/--- SM=NUL  ...(unallocated)
      UNUSED SPACE AT END
Termination Reason: SIGNAL 10 Bus error: 10
Terminating Process: exc handler [10438]

Triggered by Thread:  0


Thread 0 name:
Thread 0 Crashed:
0   libswiftCore.dylib              0x00000001a569be64 swift_unknownObjectRetain + 20 (SwiftObject.mm:511)
1   libswiftCore.dylib              0x00000001a5376d04 String._bridgeToObjectiveCImpl() + 92 (StringBridge.swift:709)
2   <App Name>                      0x0000000102bb0c20 specialized DBUtil.saveVehicleStatus(data:vin:) + 196
3   <App Name>                      0x0000000102b79928 DBUtil.saveVehicleStatus(data:vin:) + 20 (<compiler-generated>:0)
4   <App Name>                      0x0000000102b79928 closure #1 in HomeViewController.startInsertTimer() + 120 (HomeViewController.swift:642)
5   <App Name>                      0x0000000102b70d18 thunk for @escaping @callee_guaranteed @Sendable (@guaranteed NSTimer) -> () + 52 (<compiler-generated>:0)
6   Foundation                      0x00000001ab593cec __NSFireTimer + 96 (NSTimer.m:280)
7   CoreFoundation                  0x00000001abf5f3c8 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 32 (CFRunLoop.c:1797)
8   CoreFoundation                  0x00000001abf5f070 __CFRunLoopDoTimer + 1004 (CFRunLoop.c:2404)
9   CoreFoundation                  0x00000001abee8c04 __CFRunLoopDoTimers + 288 (CFRunLoop.c:2562)
10  CoreFoundation                  0x00000001abee5c1c __CFRunLoopRun + 1856 (CFRunLoop.c:3122)
11  CoreFoundation                  0x00000001abee53f8 CFRunLoopRunSpecific + 608 (CFRunLoop.c:3420)
12  GraphicsServices                0x00000001ef4734f8 GSEventRunModal + 164 (GSEvent.c:2196)
13  UIKitCore                       0x00000001ae30b8a0 -[UIApplication _run] + 888 (UIApplication.m:3685)
...

This is the line that caused the issue

private func startInsertTimer() {
    insertTimer?.invalidate()
    insertTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
        //TODO: - Code data insertion

        if let data = VDSUtil.shared.vdsString, let vin = VDSUtil.shared.vin {
            DBUtil.shared.saveVehicleStatus(data: data, vin: vin)
        }
        
    }
}

and this is my singleton struct to save data to core data

import Foundation
import CoreData

struct DBUtil {
    static let shared = DBUtil()
    private let encoder = JSONEncoder()
    
    //MARK: - Public functions
    func saveVehicleStatus(data:String, vin:String) {
        let managedContext = AppDelegate.sharedAppDelegate.coreDataStack.managedContext
        let vStatus = VehicleStatus(context: managedContext)
        vStatus.data = data
        vStatus.vin = vin
        vStatus.date = Date()
        AppDelegate.sharedAppDelegate.coreDataStack.saveContext()
    }
}

I never encounter this crash during development & testing and I can't reproduce the crash on my iPhone. Is there anything I can do to fix this bug?

0

There are 0 answers