EXC_BAD_ACCESS KERN_INVALID_ADDRESS ios 11.2 swift 4.0

855 Views Asked by At

I am facing crash in the following code only in iOS 11.2. I am using swift 4.0. I tried to debug for hours to reproduce the crash but could not succeed. Here is the code

func prepareNewConnection(conn:String) -> RTCPeerConnection {

    let uuid = UIDevice.current.identifierForVendor?.uuidString

    localAudioTrack = peerConnectionFactory.audioTrack(withTrackId: uuid!)

        mediaStream = peerConnectionFactory.mediaStream(withStreamId: LOCAL_MEDIA_STREAM_ID)
        if(localAudioTrack != nil && mediaStream != nil)
        {
            mediaStream.addAudioTrack(localAudioTrack!) //Crash on this line
        }

    let pc = peerConnectionFactory.peerConnection(with: rtcConfig, constraints: mediaConstraints, delegate: self)
    if(mediaStream != nil)
    {
        pc.add(mediaStream)
    }

return pc;
}

Here is crashlytics report.

enter image description here

I shall be very thankful for any help.

1

There are 1 best solutions below

0
rabeeh On

please avoid safely unwrapping, use if let ..

 func prepareNewConnection(conn:String) -> RTCPeerConnection {

   if let uuid = UIDevice.current.identifierForVendor?.uuidString {
localAudioTrack = peerConnectionFactory.audioTrack(withTrackId: uuid) //removed safely Unwrapping
    mediaStream = peerConnectionFactory.mediaStream(withStreamId: LOCAL_MEDIA_STREAM_ID)
 let pc = peerConnectionFactory.peerConnection(with: rtcConfig,constraints: mediaConstraints, delegate: self)
    if(localAudioTrack != nil && mediaStream != nil)
    {
        mediaStream.addAudioTrack(localAudioTrack)
        pc.add(mediaStream) 
    }
  }
 return pc
}