App crashes when trying to range beacons after working fine for 3-4 days

809 Views Asked by At

I have this beacon app that - much like everyone else's beacon app - monitors a certain number of regions, and once it enters a region, it starts ranging for 3 seconds and then stops ranging and continues monitoring. This all happens in the background. It saves the data of the nearest beacon to the local database, and sends the data to a server as well.

The weird thing is that this works fine for about 3-4 days, and then suddenly it stops working. When I open the app again to see what happened, the app seems to crash.

The crash is of type SIGABRT.

Have any of your experienced this?

Here's the most relevant code:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    locationManager = CLLocationManager()
    locationManager!.delegate = self
    locationManager!.pausesLocationUpdatesAutomatically = true


    if(application.respondsToSelector("registerUserNotificationSettings:")) {
        application.registerUserNotificationSettings(
            UIUserNotificationSettings(
                forTypes: UIUserNotificationType.Alert | UIUserNotificationType.Sound,
                categories: nil
            )
        )
    }

    return true
}

Once the user presses the "Start" button, startLocationManager() gets called:

func startLocationManager() {
    if(locationManager!.respondsToSelector("requestAlwaysAuthorization")) {
        locationManager!.requestAlwaysAuthorization()
    }

    bluetoothCentral = CBCentralManager(delegate: self, queue: nil)
}

func centralManagerDidUpdateState(central: CBCentralManager!) {
        // Determine the state of the peripheral
        if (bluetoothCentral!.state == CBCentralManagerState.PoweredOff) {
            NSLog("CoreBluetooth BLE hardware is powered off");

        }
        else if (bluetoothCentral!.state == CBCentralManagerState.PoweredOn) {
            NSLog("CoreBluetooth BLE hardware is powered on and ready");

            let prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
            let uuidString = prefs.stringForKey("UUID") as String!
            let companyId: Int = prefs.integerForKey("COMPANY_ID") as Int
            let major: CLBeaconMajorValue = CLBeaconMajorValue(companyId)
            let uuid : NSUUID? = NSUUID(UUIDString: uuidString as String)

            beaconRegionGeneral = CLBeaconRegion(proximityUUID: NSUUID(UUIDString: uuidString), identifier: "general")

            for index in 1...20 {
                beaconRegions.append(CLBeaconRegion(proximityUUID: NSUUID(UUIDString: uuidString), major: CLBeaconMajorValue(index), identifier: String(index)))
            }

            startRanging()
        }
        else if (bluetoothCentral!.state == CBCentralManagerState.Unauthorized) {
            NSLog("CoreBluetooth BLE state is unauthorized");
        }
        else if (bluetoothCentral!.state == CBCentralManagerState.Unknown) {
            NSLog("CoreBluetooth BLE state is unknown");
        }
        else if (bluetoothCentral!.state == CBCentralManagerState.Unsupported) {
            NSLog("CoreBluetooth BLE hardware is unsupported on this platform");
        }
    }

func startRanging() {
    if isRanging() {
        stopRanging()

        count = 1

        locationManager!.startRangingBeaconsInRegion(beaconRegionGeneral)
        setIsRanging(true)

    } else {
        locationManager!.startRangingBeaconsInRegion(beaconRegionGeneral)
        setIsRanging(true)
    }

}

func stopRanging() {
    locationManager!.stopRangingBeaconsInRegion(beaconRegionGeneral)
    setIsRanging(false)
}

func startMonitoring() {
    if (!isMonitoring()) {
        for beaconRegion in beaconRegions {
            locationManager!.startMonitoringForRegion(beaconRegion)
        }
        setIsMonitoring(true)
    }
}

func stopMonitoring() {
    if (isMonitoring()) {
        for beaconRegion in beaconRegions {
            locationManager!.stopMonitoringForRegion(beaconRegion)
        }
        setIsMonitoring(false)
    }
}

func locationManager(manager: CLLocationManager!, didRangeBeacons beacons: [AnyObject]!, inRegion region: CLBeaconRegion!) {
    println("Ranged \(beacons.count) beacons")
    NSLog("didRangeBeacons");

    var max: Float = -1000
    var closestBeacon: SmartBeacon = SmartBeacon()
    for beacon in beacons {
        println("Major \(beacon.major), minor \(beacon.minor), rssi \(beacon.rssi)")

        var beaconHasAlreadyBeenFound = false

        if (beacon.rssi != 0) {
            if (smartBeacons.count > 0) {
                for foundSmartBeacon in smartBeacons {
                    if (foundSmartBeacon.minor == beacon.minor) {
                        foundSmartBeacon.signalReadings.append(beacon.rssi)
                        break
                    }
                    var smartBeacon = SmartBeacon()
                    smartBeacon.major = beacon.major as Int
                    smartBeacon.minor = beacon.minor as Int
                    smartBeacon.signalReadings.append(beacon.rssi)
                    smartBeacons.append(smartBeacon)
                }

            } else {
                var smartBeacon = SmartBeacon()
                smartBeacon.major = beacon.major as Int
                smartBeacon.minor = beacon.minor as Int
                smartBeacon.signalReadings.append(beacon.rssi)
                smartBeacons.append(smartBeacon)
            }
        }
    }

    if (count == numberOfScans) {
        stopRanging()
        startMonitoring()

        let db: DAO = DAO()

        if (smartBeacons.count < 1) {

            if !newActivity {
                // Save the stopTime of the last activity to now
                saveDataOfLastActivity()
            }

            saveData(-1)
            NSLog("saveData -1")

            count = 1

            uploadLatestData()

        } else {
            for smartBeacon in smartBeacons {
                if smartBeacon.getAverageSignal() > max {
                    max = smartBeacon.getAverageSignal()
                    closestBeacon = smartBeacon
                }
            }

            if !newActivity {
                // Save the stopTime of the last activity to now
                saveDataOfLastActivity()
            }

            saveData(closestBeacon.major)
            NSLog("saveData +1")

            count = 1

            uploadLatestData()
        }

        smartBeacons.removeAll(keepCapacity: false)

        // Inform UI
        NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: nil)

    } else {
        count++
    }
}

func locationManager(manager: CLLocationManager!, didEnterRegion region: CLRegion!) {
    NSLog("Did enter region")
    sendLocalNotificationWithMessage("Did enter region \(region.identifier)", playSound: true)

    startRanging()
}

func locationManager(manager: CLLocationManager!, didExitRegion region: CLRegion!) {
    NSLog("Did exit region")
    sendLocalNotificationWithMessage("Did exit region \(region.identifier)", playSound: true)

    startRanging()
}

Here's the symbolicated crash log. The app is called "Time-Cloud".

Incident Identifier: 2DA26380-152E-4C2B-B1FA-AC6DAE522212
CrashReporter Key:   22878ad468dbc64ddf02b8e896e910e5c4bbfe59
Hardware Model:      iPad4,1
Process:             Time-Cloud [2981]
Path:                /private/var/mobile/Containers/Bundle/Application/3408977A-362E-46DE-B78E-6887B0241D21/Time-Cloud.app/Time-Cloud
Identifier:          com.time-cloud.Time-Cloud
Version:             1 (0.8.8)
Code Type:           ARM-64 (Native)
Parent Process:      launchd [1]

Date/Time:           2015-01-26 12:32:21.576 +0100
Launch Time:         2015-01-26 12:32:21.146 +0100
OS Version:          iOS 8.1 (12B410)
Report Version:      105

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Triggered by Thread:  0

Last Exception Backtrace:
0   CoreFoundation                  0x184761e48 __exceptionPreprocess + 132
1   libobjc.A.dylib                 0x1951640e4 objc_exception_throw + 60
2   CoreFoundation                  0x184761d08 +[NSException raise:format:arguments:] + 116
3   Foundation                      0x1855e5554 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 112
4   CoreLocation                    0x184eae574 0x184ea4000 + 42356
5   Time-Cloud                      0x100061500 Time_Cloud.AppDelegate.startRanging (Time_Cloud.AppDelegate)() -> () (AppDelegate.swift:201)
6   Time-Cloud                      0x100061594 @objc Time_Cloud.AppDelegate.startRanging (Time_Cloud.AppDelegate)() -> () (AppDelegate.swift:0)
7   Time-Cloud                      0x100066608 Time_Cloud.AppDelegate.locationManager (Time_Cloud.AppDelegate)(ObjectiveC.CLLocationManager!, didExitRegion : ObjectiveC.CLRegion!) -> () (AppDelegate.swift:331)
8   Time-Cloud                      0x1000667b0 @objc Time_Cloud.AppDelegate.locationManager (Time_Cloud.AppDelegate)(ObjectiveC.CLLocationManager!, didExitRegion : ObjectiveC.CLRegion!) -> () (AppDelegate.swift:0)
9   CoreLocation                    0x184eb02e4 0x184ea4000 + 49892
10  CoreLocation                    0x184eac1cc 0x184ea4000 + 33228
11  CoreLocation                    0x184ea6f00 0x184ea4000 + 12032
12  CoreFoundation                  0x18471a124 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 20
13  CoreFoundation                  0x18471922c __CFRunLoopDoBlocks + 312
14  CoreFoundation                  0x18471742c __CFRunLoopRun + 696
15  CoreFoundation                  0x1846451f4 CFRunLoopRunSpecific + 396
16  GraphicsServices                0x18d7975a4 GSEventRunModal + 168
17  UIKit                           0x188f76784 UIApplicationMain + 1488
18  Time-Cloud                      0x100068878 top_level_code (AppDelegate.swift:0)
19  Time-Cloud                      0x100068a30 main (AppDelegate.swift:0)
20  libdyld.dylib                   0x1957d2a08 start + 4


Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   libsystem_kernel.dylib          0x00000001958eb270 __pthread_kill + 8
1   libsystem_pthread.dylib         0x0000000195989224 pthread_kill + 108
2   libsystem_c.dylib               0x0000000195862b14 abort + 108
3   libc++abi.dylib                 0x0000000194949414 abort_message + 112
4   libc++abi.dylib                 0x0000000194968b88 default_terminate_handler() + 300
5   libobjc.A.dylib                 0x00000001951643bc _objc_terminate() + 124
6   libc++abi.dylib                 0x0000000194965bb0 std::__terminate(void (*)()) + 12
7   libc++abi.dylib                 0x0000000194965738 __cxa_rethrow + 140
8   libobjc.A.dylib                 0x0000000195164290 objc_exception_rethrow + 40
9   CoreFoundation                  0x00000001846452a0 CFRunLoopRunSpecific + 568
10  GraphicsServices                0x000000018d7975a0 GSEventRunModal + 164
11  UIKit                           0x0000000188f76780 UIApplicationMain + 1484
12  Time-Cloud                      0x0000000100068874 top_level_code (AppDelegate.swift:0)
13  Time-Cloud                      0x0000000100068a2c main (AppDelegate.swift:0)
14  libdyld.dylib                   0x00000001957d2a04 start + 0

Thread 1 name:  Dispatch queue: com.apple.libdispatch-manager
Thread 1:
0   libsystem_kernel.dylib          0x00000001958d0c94 kevent64 + 8
1   libdispatch.dylib               0x00000001957b897c _dispatch_mgr_invoke + 272
2   libdispatch.dylib               0x00000001957ab3b0 _dispatch_mgr_thread + 48

Thread 2:
0   libsystem_kernel.dylib          0x00000001958ebc78 __workq_kernreturn + 8
1   libsystem_pthread.dylib         0x0000000195985390 _pthread_wqthread + 988
2   libsystem_pthread.dylib         0x0000000195984fa4 start_wqthread + 0

Thread 3 name:  Dispatch queue: com.apple.networking.connection.0x1006389a0
Thread 3:
0   libsystem_platform.dylib        0x000000019597da80 OSAtomicDequeue + 12
1   libsystem_malloc.dylib          0x0000000195926450 _nano_malloc_check_clear + 108
2   libsystem_malloc.dylib          0x00000001959250d8 nano_calloc + 76
3   libsystem_malloc.dylib          0x0000000195919958 malloc_zone_calloc + 120
4   libsystem_malloc.dylib          0x00000001959198b8 calloc + 60
5   libobjc.A.dylib                 0x000000019516fc20 class_createInstance + 76
6   libdispatch.dylib               0x00000001957a93e8 _os_object_alloc_realized + 36
7   libdispatch.dylib               0x00000001957aabc0 dispatch_source_create + 160
8   libsystem_dnssd.dylib           0x00000001958a543c DNSServiceSetDispatchQueue + 156
9   libsystem_network.dylib         0x000000019594d4a4 tcp_connection_host_start + 848
10  libsystem_network.dylib         0x0000000195935234 tcp_connection_start_host + 292
11  libsystem_network.dylib         0x000000019593e898 tcp_connection_start_direct_connect + 1068
12  libsystem_network.dylib         0x000000019593e030 tcp_connection_handle_reachability_changed + 3196
13  libsystem_network.dylib         0x000000019593b580 __tcp_connection_start_block_invoke + 340
14  libdispatch.dylib               0x00000001957a93a8 _dispatch_call_block_and_release + 20
15  libdispatch.dylib               0x00000001957a9368 _dispatch_client_callout + 12
16  libdispatch.dylib               0x00000001957b34bc _dispatch_queue_drain + 1212
17  libdispatch.dylib               0x00000001957ac470 _dispatch_queue_invoke + 128
18  libdispatch.dylib               0x00000001957b5220 _dispatch_root_queue_drain + 660
19  libdispatch.dylib               0x00000001957b6758 _dispatch_worker_thread3 + 104
20  libsystem_pthread.dylib         0x00000001959852e0 _pthread_wqthread + 812
21  libsystem_pthread.dylib         0x0000000195984fa4 start_wqthread + 0

Thread 4 name:  Dispatch queue: com.apple.CoreLocation.ConnectionClient.0x174141340.events
Thread 4:
0   libsystem_kernel.dylib          0x00000001958d0ed0 semaphore_timedwait_trap + 8
1   libdispatch.dylib               0x00000001957b69f4 _dispatch_semaphore_wait_slow + 164
2   CoreLocation                    0x0000000184ea6e08 0x184ea4000 + 11784
3   CoreLocation                    0x0000000184eaaf8c 0x184ea4000 + 28556
4   CoreLocation                    0x0000000184edd97c 0x184ea4000 + 235900
5   libxpc.dylib                    0x00000001959acd1c _xpc_connection_call_event_handler + 64
6   libxpc.dylib                    0x00000001959aac78 _xpc_connection_mach_event + 2156
7   libdispatch.dylib               0x00000001957a943c _dispatch_client_callout4 + 12
8   libdispatch.dylib               0x00000001957acb54 _dispatch_mach_msg_invoke + 488
9   libdispatch.dylib               0x00000001957b32a0 _dispatch_queue_drain + 672
10  libdispatch.dylib               0x00000001957ac07c _dispatch_mach_invoke + 132
11  libdispatch.dylib               0x00000001957b32a0 _dispatch_queue_drain + 672
12  libdispatch.dylib               0x00000001957ac470 _dispatch_queue_invoke + 128
13  libdispatch.dylib               0x00000001957b32a0 _dispatch_queue_drain + 672
14  libdispatch.dylib               0x00000001957ac470 _dispatch_queue_invoke + 128
15  libdispatch.dylib               0x00000001957b5220 _dispatch_root_queue_drain + 660
16  libdispatch.dylib               0x00000001957b6758 _dispatch_worker_thread3 + 104
17  libsystem_pthread.dylib         0x00000001959852e0 _pthread_wqthread + 812
18  libsystem_pthread.dylib         0x0000000195984fa4 start_wqthread + 0

Thread 5 name:  Dispatch queue: com.apple.root.default-qos
Thread 5:
0   libsystem_kernel.dylib          0x00000001958d0eb8 semaphore_wait_trap + 8
1   libdispatch.dylib               0x00000001957b6a4c _dispatch_semaphore_wait_slow + 252
2   CFNetwork                       0x00000001841294e0 CFURLConnectionSendSynchronousRequest + 284
3   CFNetwork                       0x0000000184149bc8 +[NSURLConnection sendSynchronousRequest:returningResponse:error:] + 116
4   Time-Cloud                      0x0000000100039080 Time_Cloud.SendDataToCloudTask.send (Time_Cloud.SendDataToCloudTask)([[Swift.String : Swift.String]]) -> () (SendDataToCloudTask.swift:113)
5   Time-Cloud                      0x0000000100035664 Time_Cloud.ViewController.(uploadLatestData (Time_Cloud.ViewController) -> () -> ()).(closure #1) (ViewController.swift:844)
6   Time-Cloud                      0x000000010000f490 reabstraction thunk helper from @callee_owned () -> (@unowned ()) to @callee_unowned @objc_block () -> (@unowned ()) (SettingsViewController.swift:0)
7   libdispatch.dylib               0x00000001957a93a8 _dispatch_call_block_and_release + 20
8   libdispatch.dylib               0x00000001957a9368 _dispatch_client_callout + 12
9   libdispatch.dylib               0x00000001957b5408 _dispatch_root_queue_drain + 1148
10  libdispatch.dylib               0x00000001957b6758 _dispatch_worker_thread3 + 104
11  libsystem_pthread.dylib         0x00000001959852e0 _pthread_wqthread + 812
12  libsystem_pthread.dylib         0x0000000195984fa4 start_wqthread + 0

Thread 6:
0   libsystem_kernel.dylib          0x00000001958ebc78 __workq_kernreturn + 8
1   libsystem_pthread.dylib         0x0000000195985390 _pthread_wqthread + 988
2   libsystem_pthread.dylib         0x0000000195984fa4 start_wqthread + 0

Thread 7:
0   libsystem_kernel.dylib          0x00000001958ebc78 __workq_kernreturn + 8
1   libsystem_pthread.dylib         0x0000000195985390 _pthread_wqthread + 988
2   libsystem_pthread.dylib         0x0000000195984fa4 start_wqthread + 0

Thread 8 name:  com.apple.NSURLConnectionLoader
Thread 8:
0   libsystem_kernel.dylib          0x00000001958d0e7c mach_msg_trap + 8
1   libsystem_kernel.dylib          0x00000001958d0cf4 mach_msg + 68
2   CoreFoundation                  0x00000001847195c8 __CFRunLoopServiceMachPort + 196
3   CoreFoundation                  0x000000018471751c __CFRunLoopRun + 936
4   CoreFoundation                  0x00000001846451f0 CFRunLoopRunSpecific + 392
5   CFNetwork                       0x00000001841496a0 +[NSURLConnection(Loader) _resourceLoadLoop:] + 436
6   Foundation                      0x0000000185631c08 __NSThread__main__ + 1068
7   libsystem_pthread.dylib         0x0000000195987e7c _pthread_body + 160
8   libsystem_pthread.dylib         0x0000000195987dd8 _pthread_start + 156
9   libsystem_pthread.dylib         0x0000000195984fac thread_start + 0

Thread 9:
0   libsystem_kernel.dylib          0x00000001958ebc78 __workq_kernreturn + 8
1   libsystem_pthread.dylib         0x0000000195985390 _pthread_wqthread + 988
2   libsystem_pthread.dylib         0x0000000195984fa4 start_wqthread + 0

Thread 0 crashed with ARM Thread State (64-bit):
    x0: 0x0000000000000000   x1: 0x0000000000000000   x2: 0x0000000000000000   x3: 0x00000001700e0737
    x4: 0x0000000194969dfd   x5: 0x000000016fdff3e0   x6: 0x000000000000006e   x7: 0x0000000000000f80
    x8: 0x0000000008000000   x9: 0x0000000004000000  x10: 0x0000000000000000  x11: 0x0000000000000000
   x12: 0x0000000000000000  x13: 0x0000000000000000  x14: 0x0000000000000002  x15: 0x0000000000000000
   x16: 0x0000000000000148  x17: 0x0000000000000000  x18: 0x0000000000000000  x19: 0x0000000000000006
   x20: 0x00000001994aa310  x21: 0x0000000199492ce8  x22: 0x00000001741402d0  x23: 0x0000000195de67c0
   x24: 0x0000000000000001  x25: 0x00000001741402c0  x26: 0x00000001994910a8  x27: 0x0000000000000001
   x28: 0x0000000000000000  fp: 0x000000016fdff340   lr: 0x0000000195989228
    sp: 0x000000016fdff320   pc: 0x00000001958eb270 cpsr: 0x00000000
0

There are 0 best solutions below