关闭内的EXC_BAD_ACCESS KERN_INVALID_ADDRESS

时间:2017-08-08 03:34:25

标签: ios swift crash exc-bad-access

我写了这个类来跟踪设备的加速度。

import CoreMotion
import Foundation

protocol MotionManagerDelegate {
    func didReceiveAcceleration(_ acceleration: (x: Double, y: Double, z: Double))
}


class MotionManager: NSObject {

    var delegate: MotionManagerDelegate?

    fileprivate let motionDetector = SOMotionDetector.sharedInstance()
    fileprivate let accelerationCaptureInterval: TimeInterval = 0.02
    fileprivate let gyroCaptureInterval: TimeInterval = 1
    fileprivate var isMoving: Bool = false

    fileprivate var motionManager: CMMotionManager!


    override init() {
        super.init()

        motionManager = CMMotionManager()
        motionManager.gyroUpdateInterval = gyroCaptureInterval
        motionManager.accelerometerUpdateInterval = accelerationCaptureInterval
        motionManager.deviceMotionUpdateInterval = gyroCaptureInterval
    }

    func startCapturing() throws {
        // ...

        motionManager.startAccelerometerUpdates(to: OperationQueue()) { accelerometerData, error in
            var x = 0.0
            var y = 0.0
            var z = 0.0
            if let acceleration = accelerometerData?.acceleration {
                x = acceleration.x
                y = acceleration.y
                z = acceleration.z
            }

            if self.isMoving {
                self.delegate?.didReceiveAcceleration((x: x, y: y, z: z)) // line 77
            }
        }

        // ...
    }

    func stopCapturing() {
        motionManager.stopGyroUpdates()
        motionManager.stopAccelerometerUpdates()
        motionManager.stopDeviceMotionUpdates()
    }
}

99%的时间都有效。但偶尔会发生以下崩溃。

  

坠毁:NSOperationQueue 0x170a20e40 :: NSOperation 0x17084e8e0(QOS:   DEFAULT)EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x0000000000000000

它随机发生,因此很难用Xcode调试它。我添加了Crashlytics并获得了崩溃报告。

# OS Version: 10.2.1 (14D27)
# Device: iPhone 7
# RAM Free: 30.5%
# Disk Free: 93.6%

#14. Crashed: NSOperationQueue 0x170a20e40 :: NSOperation 0x17084e8e0 (QOS: DEFAULT)
0  libswiftCore.dylib             0x10142c398 _swift_release_dealloc + 4
1  CycleSourcing (Alpha)          0x1000e3294 specialized Array._copyToNewBuffer(oldCount : Int) -> () + 4295979668
2  CycleSourcing (Alpha)          0x1000e2f08 BumpDetectorAlgorithm.detectStop(withAcceleration : (x : Double, y : Double, z : Double)) -> () (BumpDetectorAlgorithm.swift)
3  CycleSourcing (Alpha)          0x1000d0b18 protocol witness for MotionManagerDelegate.didReceiveAcceleration(x : Double, y : Double, z : Double) -> () in conformance DashboardViewController (DashboardViewController.swift)
4  CycleSourcing (Alpha)          0x1000eb7e8 specialized MotionManager.(startCapturing() throws -> ()).(closure #3) (MotionManager.swift:77)
5  CycleSourcing (Alpha)          0x1000eb4a8 partial apply for MotionManager.(startCapturing() throws -> ()).(closure #1) + 4296012968
6  Foundation                     0x1845e3fb0 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 16
7  Foundation                     0x184528aa8 -[NSBlockOperation main] + 96
8  Foundation                     0x1845190a4 -[__NSOperationInternal _start:] + 620
9  Foundation                     0x1845e635c __NSOQSchedule_f + 228
10 libdispatch.dylib              0x18297e1bc _dispatch_client_callout + 16
11 libdispatch.dylib              0x18298c3dc _dispatch_queue_serial_drain + 928
12 libdispatch.dylib              0x1829819a4 _dispatch_queue_invoke + 652
13 libdispatch.dylib              0x18298e34c _dispatch_root_queue_drain + 572
14 libdispatch.dylib              0x18298e0ac _dispatch_worker_thread3 + 124
15 libsystem_pthread.dylib        0x182b872a0 _pthread_wqthread + 1288
16 libsystem_pthread.dylib        0x182b86d8c start_wqthread + 4

--

#0. com.apple.main-thread
0  libsystem_kernel.dylib         0x182aa5188 mach_msg_trap + 8
1  libsystem_kernel.dylib         0x182aa4ff8 mach_msg + 72
2  CoreFoundation                 0x183aa25d0 __CFRunLoopServiceMachPort + 192
3  CoreFoundation                 0x183aa01ec __CFRunLoopRun + 1132
4  CoreFoundation                 0x1839ce2b8 CFRunLoopRunSpecific + 444
5  GraphicsServices               0x185482198 GSEventRunModal + 180
6  UIKit                          0x189a157fc -[UIApplication _run] + 684
7  UIKit                          0x189a10534 UIApplicationMain + 208
8  CycleSourcing (Alpha)          0x10009eba0 main (AppDelegate.swift:16)
9  libdispatch.dylib              0x1829b15b8 (Missing)

#1. com.apple.uikit.eventfetch-thread
0  libsystem_kernel.dylib         0x182aa5188 mach_msg_trap + 8
1  libsystem_kernel.dylib         0x182aa4ff8 mach_msg + 72
2  CoreFoundation                 0x183aa25d0 __CFRunLoopServiceMachPort + 192
3  CoreFoundation                 0x183aa01ec __CFRunLoopRun + 1132
4  CoreFoundation                 0x1839ce2b8 CFRunLoopRunSpecific + 444
5  Foundation                     0x18450b26c -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 304
6  Foundation                     0x18452bdd0 -[NSRunLoop(NSRunLoop) runUntilDate:] + 96
7  UIKit                          0x18a389c38 -[UIEventFetcher threadMain] + 136
8  Foundation                     0x184608e68 __NSThread__start__ + 1024
9  libsystem_pthread.dylib        0x182b89850 _pthread_body + 240
10 libsystem_pthread.dylib        0x182b89760 _pthread_body + 282
11 libsystem_pthread.dylib        0x182b86d94 thread_start + 4

#2. com.twitter.crashlytics.ios.MachExceptionServer
0  CycleSourcing (Alpha)          0x1001116b0 CLSProcessRecordAllThreads + 4296169136
1  CycleSourcing (Alpha)          0x1001116b0 CLSProcessRecordAllThreads + 4296169136
2  CycleSourcing (Alpha)          0x10011156c CLSProcessRecordAllThreads + 4296168812
3  CycleSourcing (Alpha)          0x100101688 CLSHandler + 4296103560
4  CycleSourcing (Alpha)          0x1000fc69c CLSMachExceptionServer + 4296083100
5  libsystem_pthread.dylib        0x182b89850 _pthread_body + 240
6  libsystem_pthread.dylib        0x182b89760 _pthread_body + 282
7  libsystem_pthread.dylib        0x182b86d94 thread_start + 4

#3. com.apple.NSURLConnectionLoader
0  libsystem_kernel.dylib         0x182aa5188 mach_msg_trap + 8
1  libsystem_kernel.dylib         0x182aa4ff8 mach_msg + 72
2  CoreFoundation                 0x183aa25d0 __CFRunLoopServiceMachPort + 192
3  CoreFoundation                 0x183aa01ec __CFRunLoopRun + 1132
4  CoreFoundation                 0x1839ce2b8 CFRunLoopRunSpecific + 444
5  CFNetwork                      0x1841d3a70 +[NSURLConnection(Loader) _resourceLoadLoop:] + 336
6  Foundation                     0x184608e68 __NSThread__start__ + 1024
7  libsystem_pthread.dylib        0x182b89850 _pthread_body + 240
8  libsystem_pthread.dylib        0x182b89760 _pthread_body + 282
9  libsystem_pthread.dylib        0x182b86d94 thread_start + 4

#4. com.apple.CoreMotion.MotionThread
0  libsystem_malloc.dylib         0x182af8e5c tiny_malloc_from_free_list + 144
1  libsystem_malloc.dylib         0x182af751c szone_malloc_should_clear + 512
2  libsystem_malloc.dylib         0x182af751c szone_malloc_should_clear + 512
3  libsystem_malloc.dylib         0x182afa168 malloc_zone_calloc + 152
4  libsystem_malloc.dylib         0x182afa0ac calloc + 40
5  libobjc.A.dylib                0x182537624 class_createInstance + 76
6  libobjc.A.dylib                0x182545ae4 _objc_rootAlloc + 52
7  Foundation                     0x184518ac4 -[NSOperation init] + 524
8  Foundation                     0x184527800 -[NSBlockOperation init] + 48
9  Foundation                     0x184566fb0 -[NSBlockOperation initWithBlock:] + 40
10 Foundation                     0x184566f74 +[NSBlockOperation blockOperationWithBlock:] + 44
11 Foundation                     0x18456cf7c -[NSOperationQueue addOperationWithBlock:] + 36
12 CoreMotion                     0x18a86eec8 (null) + 63332
13 CoreMotion                     0x18a88a6d4 (null) + 175984
14 CoreMotion                     0x18a885ee4 (null) + 157568
15 CoreMotion                     0x18a88d72c (null) + 188360
16 CoreMotion                     0x18a8842f0 (null) + 150412
17 IOKit                          0x183d89d28 __IOHIDEventSystemClientQueueCallback + 444
18 CoreFoundation                 0x183a8ae50 __CFMachPortPerform + 180
19 CoreFoundation                 0x183aa3218 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 56
20 CoreFoundation                 0x183aa29cc __CFRunLoopDoSource1 + 436
21 CoreFoundation                 0x183aa04b0 __CFRunLoopRun + 1840
22 CoreFoundation                 0x1839ce2b8 CFRunLoopRunSpecific + 444
23 CoreFoundation                 0x183a1bb44 CFRunLoopRun + 112
24 CoreMotion                     0x18a891120 (null) + 203196
25 libsystem_pthread.dylib        0x182b89850 _pthread_body + 240
26 libsystem_pthread.dylib        0x182b89760 _pthread_body + 282
27 libsystem_pthread.dylib        0x182b86d94 thread_start + 4

#5. AVAudioSession Notify Thread
0  libsystem_kernel.dylib         0x182aa5188 mach_msg_trap + 8
1  libsystem_kernel.dylib         0x182aa4ff8 mach_msg + 72
2  CoreFoundation                 0x183aa25d0 __CFRunLoopServiceMachPort + 192
3  CoreFoundation                 0x183aa01ec __CFRunLoopRun + 1132
4  CoreFoundation                 0x1839ce2b8 CFRunLoopRunSpecific + 444
5  AVFAudio                       0x19d4d3d24 GenericRunLoopThread::Entry(void*) + 164
6  AVFAudio                       0x19d4f9d9c CAPThread::Entry(CAPThread*) + 84
7  libsystem_pthread.dylib        0x182b89850 _pthread_body + 240
8  libsystem_pthread.dylib        0x182b89760 _pthread_body + 282
9  libsystem_pthread.dylib        0x182b86d94 thread_start + 4

#6. Thread
0  libsystem_kernel.dylib         0x182ac3a88 __workq_kernreturn + 8
1  libsystem_pthread.dylib        0x182b87160 _pthread_wqthread + 968
2  libsystem_pthread.dylib        0x182b86d8c start_wqthread + 4

#7. Thread
0  libsystem_kernel.dylib         0x182ac3a88 __workq_kernreturn + 8
1  libsystem_pthread.dylib        0x182b87344 _pthread_wqthread + 1452
2  libsystem_pthread.dylib        0x182b86d8c start_wqthread + 4

#8. Thread
0  libsystem_kernel.dylib         0x182ac3a88 __workq_kernreturn + 8
1  libsystem_pthread.dylib        0x182b87344 _pthread_wqthread + 1452
2  libsystem_pthread.dylib        0x182b86d8c start_wqthread + 4

#9. Thread
0  libsystem_kernel.dylib         0x182ac3a88 __workq_kernreturn + 8
1  libsystem_pthread.dylib        0x182b87344 _pthread_wqthread + 1452
2  libsystem_pthread.dylib        0x182b86d8c start_wqthread + 4

#10. Thread
0  libsystem_kernel.dylib         0x182ac3a88 __workq_kernreturn + 8
1  libsystem_pthread.dylib        0x182b87344 _pthread_wqthread + 1452
2  libsystem_pthread.dylib        0x182b86d8c start_wqthread + 4

#11. Thread
0  libsystem_pthread.dylib        0x182b86d88 start_wqthread + 126

#12. Thread
0  libsystem_kernel.dylib         0x182ac3a88 __workq_kernreturn + 8
1  libsystem_pthread.dylib        0x182b87160 _pthread_wqthread + 968
2  libsystem_pthread.dylib        0x182b86d8c start_wqthread + 4

#13. Thread
0  libsystem_kernel.dylib         0x182ac3a88 __workq_kernreturn + 8
1  libsystem_pthread.dylib        0x182b87344 _pthread_wqthread + 1452
2  libsystem_pthread.dylib        0x182b86d8c start_wqthread + 4

#14. Crashed: NSOperationQueue 0x170a20e40 :: NSOperation 0x17084e8e0 (QOS: DEFAULT)
0  libswiftCore.dylib             0x10142c398 _swift_release_dealloc + 4
1  CycleSourcing (Alpha)          0x1000e3294 specialized Array._copyToNewBuffer(oldCount : Int) -> () + 4295979668
2  CycleSourcing (Alpha)          0x1000e2f08 BumpDetectorAlgorithm.detectStop(withAcceleration : (x : Double, y : Double, z : Double)) -> () (BumpDetectorAlgorithm.swift)
3  CycleSourcing (Alpha)          0x1000d0b18 protocol witness for MotionManagerDelegate.didReceiveAcceleration(x : Double, y : Double, z : Double) -> () in conformance DashboardViewController (DashboardViewController.swift)
4  CycleSourcing (Alpha)          0x1000eb7e8 specialized MotionManager.(startCapturing() throws -> ()).(closure #3) (MotionManager.swift:77)
5  CycleSourcing (Alpha)          0x1000eb4a8 partial apply for MotionManager.(startCapturing() throws -> ()).(closure #1) + 4296012968
6  Foundation                     0x1845e3fb0 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 16
7  Foundation                     0x184528aa8 -[NSBlockOperation main] + 96
8  Foundation                     0x1845190a4 -[__NSOperationInternal _start:] + 620
9  Foundation                     0x1845e635c __NSOQSchedule_f + 228
10 libdispatch.dylib              0x18297e1bc _dispatch_client_callout + 16
11 libdispatch.dylib              0x18298c3dc _dispatch_queue_serial_drain + 928
12 libdispatch.dylib              0x1829819a4 _dispatch_queue_invoke + 652
13 libdispatch.dylib              0x18298e34c _dispatch_root_queue_drain + 572
14 libdispatch.dylib              0x18298e0ac _dispatch_worker_thread3 + 124
15 libsystem_pthread.dylib        0x182b872a0 _pthread_wqthread + 1288
16 libsystem_pthread.dylib        0x182b86d8c start_wqthread + 4

崩溃显然发生在self.delegate?.didReceiveAcceleration((x: x, y: y, z: z))行。不知道是什么导致了它。

0 个答案:

没有答案