我在应用程序上安装了Crashlytics,发现一个影响某些用户的错误,我无法自己复制它。

错误是EXC_BAD_ACCESS KERN_INVALID_ADDRESS。我查看了this post re: EXC_BAD_ACCESS,但无法确定崩溃日志中应该在哪里查找问题。

根据以下崩溃日志,是否应该在某个地方放置一个断点来检查nil?

    Thread 0:

Crashed: com.apple.main-thread
EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x00000000154d9050
 raw

0   libobjc.A.dylib objc_msgSend + 16
1   libAVFAudio.dylib   -[AVAudioPlayer(AVAudioPlayerPriv) finishedPlaying:] + 92
2   Foundation  __NSThreadPerformPerform + 340
3   CoreFoundation  __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
4   CoreFoundation  __CFRunLoopDoSources0 + 540
5   CoreFoundation  __CFRunLoopRun + 724
6   CoreFoundation  CFRunLoopRunSpecific + 384
7   GraphicsServices    GSEventRunModal + 180
8   UIKit   UIApplicationMain + 204
9   My App  AppDelegate.swift line 19   main
10  libdyld.dylib   start + 4

Thread 1: com.apple.libdispatch-manager

0   libsystem_kernel.dylib  kevent_qos + 8
1   libdispatch.dylib   _dispatch_mgr_invoke + 232
2   libdispatch.dylib   _dispatch_source_invoke + 50

Thread 2: com.twitter.crashlytics.ios.MachExceptionServer

0   My App  CLSProcess.c line 374   CLSProcessRecordAllThreads
1   My App  CLSProcess.c line 374   CLSProcessRecordAllThreads
2   My App  CLSProcess.c line 398   CLSProcessRecordAllThreads
3   My App  CLSHandler.m line 24    CLSHandler
4   My App  CLSMachException.c line 443 CLSMachExceptionServer
5   libsystem_pthread.dylib _pthread_body + 156
6   libsystem_pthread.dylib _pthread_body + 154
7   libsystem_pthread.dylib thread_start + 4

Thread 3: com.apple.NSURLConnectionLoader

0   libsystem_kernel.dylib  mach_msg_trap + 8
1   libsystem_kernel.dylib  mach_msg + 72
2   CoreFoundation  __CFRunLoopServiceMachPort + 196
3   CoreFoundation  __CFRunLoopRun + 1032
4   CoreFoundation  CFRunLoopRunSpecific + 384
5   CFNetwork   +[NSURLConnection(Loader) _resourceLoadLoop:] + 412
6   Foundation  __NSThread__start__ + 1000
7   libsystem_pthread.dylib _pthread_body + 156
8   libsystem_pthread.dylib _pthread_body + 154
9   libsystem_pthread.dylib thread_start + 4

Thread 4: com.apple.CFSocket.private

0   libsystem_kernel.dylib  __select + 8
1   CoreFoundation  __CFSocketManager + 648
2   libsystem_pthread.dylib _pthread_body + 156
3   libsystem_pthread.dylib _pthread_body + 154
4   libsystem_pthread.dylib thread_start + 4

Thread 5: com.apple.coreaudio.AQClient

0   libsystem_kernel.dylib  mach_msg_trap + 8
1   libsystem_kernel.dylib  mach_msg + 72
2   CoreFoundation  __CFRunLoopServiceMachPort + 196
3   CoreFoundation  __CFRunLoopRun + 1032
4   CoreFoundation  CFRunLoopRunSpecific + 384
5   AudioToolbox    GenericRunLoopThread::Entry(void*) + 164
6   AudioToolbox    CAPThread::Entry(CAPThread*) + 124
7   libsystem_pthread.dylib _pthread_body + 156
8   libsystem_pthread.dylib _pthread_body + 154
9   libsystem_pthread.dylib thread_start + 4

Thread 6: Thread

0   libsystem_kernel.dylib  __workq_kernreturn + 8
1   libsystem_pthread.dylib _pthread_wqthread + 1284
2   libsystem_pthread.dylib start_wqthread + 4

Thread 7: Thread

0   libsystem_kernel.dylib  __workq_kernreturn + 8
1   libsystem_pthread.dylib _pthread_wqthread + 1284
2   libsystem_pthread.dylib start_wqthread + 4

Thread 8: Thread

0   libsystem_kernel.dylib  __workq_kernreturn + 8
1   libsystem_pthread.dylib _pthread_wqthread + 1284
2   libsystem_pthread.dylib start_wqthread + 4

Thread 9: Thread

0   libsystem_kernel.dylib  __workq_kernreturn + 8
1   libsystem_pthread.dylib _pthread_wqthread + 1284
2   libsystem_pthread.dylib start_wqthread + 4

更新:

在Rog的建议下,我研究了调用AVAudioPlayer的方法。

我有一个叫做MP3Player的类。它使用两种初始化方法:

1)播放单个文件
init(fileName: String) {
    // Need to get location of file in bundle
    let fileNamePath: String = NSBundle.mainBundle().pathForResource(fileName, ofType: "")!
    // Then add the filename to the tracks array of strings
    tracks.append(fileNamePath)
    super.init()
    queueTrack()
}

2)播放一系列文件
init(arrayOfMP3FileNames: NSArray) {
    for fileName in arrayOfMP3FileNames {
        // get the path of the file...
        let fileNamePath: String = NSBundle.mainBundle().pathForResource(fileName as? String, ofType: "")!
        // ...and add it to the tracks array
        tracks.append(fileNamePath)
    }

    super.init()
    queueTrack()
}
queueTrack方法似乎包含了我的部分问题:
func queueTrack() {
    // I have no idea what possessed me to put this in, but I think it's the majority
    // of my problem
    // ** TOOK FOLLOWING 2 LINES OUT **
    // if (player != nil) {
    //     player =  nil
    // }

    let url = NSURL.fileURLWithPath(tracks[currentTrackIndex] as String)

    do {
        player = try AVAudioPlayer(contentsOfURL: url) // Thread 1: breakpoint 1.1
        player?.delegate = self
        player?.prepareToPlay()
    } catch let error as NSError {
        NSLog("Unresolved error \(error.debugDescription)")
        // SHOW ALERT OR SOMETHING
    }
}

在断点导航器中,我为“所有异常”添加了异常断点。如果当前正在播放声音时播放另一种声音,则do {}语句的第一行会出现异常。它不会在模拟器或设备上崩溃,但我认为我需要对其进行清理,以免发生异常。解决此问题的任何建议将不胜感激。

最佳答案

在Rog的建议下,我查看了我的MP3Player类如何播放MP3文件,并发现从该类的初始化程序调用的方法正在初始化MP3播放器实例。我最初写时以为是在“清理”,但实际上是在创造潜在崩溃的根源。

func queueTrack() {
    // I have no idea what possessed me to put this in, but I think it's the majority
    // of my problem
    // ** TOOK FOLLOWING 2 LINES OUT **
    // if (player != nil) {
    //     player =  nil
    // }

    let url = NSURL.fileURLWithPath(tracks[currentTrackIndex] as String)

    do {
        player = try AVAudioPlayer(contentsOfURL: url) // Thread 1: breakpoint 1.1
        player?.delegate = self
        player?.prepareToPlay()
    } catch let error as NSError {
        NSLog("Unresolved error \(error.debugDescription)")
        // SHOW ALERT OR SOMETHING
    }
}

10-02 18:56