title: Determining root cause of "EXC_BAD_ACCESS KERN_INVALID_ADDRESS" logged with Crashlytics
body: <p>I have Crashlytics installed on an app and I've found a bug that's affecting some users that I'm unable to replicate it myself.</p>
  
  <p>The error is <code>EXC_BAD_ACCESS KERN_INVALID_ADDRESS</code>. I looked at <a href="https://stackoverflow.com/questions/7684742/how-to-solve-kern-invalid-address-on-exc-bad-access-sigsegv">this post re: EXC_BAD_ACCESS</a>, but I can't figure out where I should be looking for the problem in my crash log.</p>
  
  <p>Based on the following crash log, is there a place where I should be putting a breakpoint to check for nils?</p>
  
  <pre><code>    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
  </code></pre>
  
  <p><strong>Update:</strong></p>
  
  <p>At Rog's suggestion, I looked at the method that calls the AVAudioPlayer.</p>
  
  <p>I have a class called <code>MP3Player</code>. It has two initialization methods that it uses:</p>
  
  <p><strong><em>1) play a single file</em></strong></p>
  
  <pre><code>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()
  }
  </code></pre>
  
  <p><strong><em>2) play a sequence of files</em></strong></p>
  
  <pre><code>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()
  }
  </code></pre>
  
  <p>The <code>queueTrack</code> method appears to have contained part of my problem:</p>
  
  <pre><code>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
      }
  }
  </code></pre>
  
  <p>In the breakpoint navigator, I added exception breakpoint for "All Exceptions". I get an exception on the first line of the <code>do {}</code> statement if I play another sound while a sound is currently playing. It doesn't crash in the simulator or on the device, but I think I need to clean it up so I don't get the exception. Any suggestions on resolving this issue would be greatly appreciated.</p>