CocoaAsyncSocket Index out of range on data object in Swift iOS app

51 Views Asked by At

One of my users receives occasionally a strange index-out-of-range exception.

This happens in the udpSocket callback of the CocoaAsyncSocket library.

func udpSocket(_ sock: GCDAsyncUdpSocket, didReceive data: Data, fromAddress address: Data, withFilterContext filterContext: Any?)
    {
        idleTimer?.invalidate()
        
        nmeaStatus = .connected
        
        guard data.count > 0 else {
            return
        }
        
        if NavFactory.shared.nmeaDiagnostic
        {
            Syslog.add(origin: .undefined, level: .info, text: "UDP received datacount: \(data.count)")
            Syslog.add(origin: .undefined, level: .info, text: "UDP received data: \(data.hexEncodedString(options: .upperCase))")
            if let datastring = String(data: data, encoding: .ascii)
            {
                Syslog.add(origin: .undefined, level: .info, text: "UDP received data: \(datastring)")
            }
        }
        
        
        processData(data: data)
    }

The Swift runtime failure: Index out of range exception happens on the call of processData(data: data)

which is defined as

 func processData(data: Data)
 {
   // A lot of stuff to process...
 }

The diagnostic outputs before work fine.

This happens only rarely and is therefore very hard to trackdown, especially as I have no access to the users setup.

The last log I received shows that the UDP connection was closed about 3 minutes before the crash happened.

Could it be that CocoaAsyncSocket still processes buffered data after the socket closed and this causes some corrupt data ?

0

There are 0 best solutions below